將 MySQL 記錄(價格值)乘以後取格式


要設定記錄格式,請使用 FORMAT()。我們先建立一個表 -

mysql> create table DemoTable
   -> (
   -> Price decimal(10,4),
   -> Rate decimal(10,4)
   -> );
Query OK, 0 rows affected (0.96 sec)

使用“insert”命令在表中插入一些記錄 -

mysql> insert into DemoTable values(1000,10.2);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values(2000,20.4);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(100,5);
Query OK, 1 row affected (0.16 sec)

使用“select”語句顯示錶中的所有記錄 -

mysql> select *from DemoTable;

這將產生以下輸出 -

+-----------+---------+
| Price     | Rate    |
+-----------+---------+
| 1000.0000 | 10.2000 |
| 2000.0000 | 20.4000 |
|  100.0000 | 5.0000  |
+-----------+---------+
3 rows in set (0.00 sec)

以下是設定記錄格式的查詢 -

mysql> select format((Price * Rate),2) from DemoTable;

這將產生以下輸出 -

+--------------------------+
| format((Price * Rate),2) |
+--------------------------+
| 10,200.00                |
| 40,800.00                |
| 500.00                   |
+--------------------------+
3 rows in set (0.00 sec)

更新於: 2019-12-17

167 次瀏覽

開啟你的職業

完成課程獲取認證。

立即開始
廣告
© . All rights reserved.