在 MySQL 中以兩位小數為千位數格式化金額值?


對於千位數,使用 MySQL FORMAT()。我們首先建立一個 −

mysql> create table DemoTable1394
   -> (
   -> Amount decimal(7,3)
   -> );
Query OK, 0 rows affected (0.68 sec)

使用 insert 在表中插入一些記錄 −

mysql> insert into DemoTable1394 values(60);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1394 values(2355.4);
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1394 values(456);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1394 values(8769);
Query OK, 1 row affected (0.13 sec)

使用 select 從表格中顯示所有記錄 −

mysql> select * from DemoTable1394;

這將產生以下輸出 −

+----------+
| Amount   |
+----------+
|   60.000 |
| 2355.400 |
|  456.000 |
| 8769.000 |
+----------+
4 rows in set (0.00 sec)

以下是使用兩位小數格式化千位數的查詢 −

mysql> select format(Amount,2) from DemoTable1394;

這將產生以下輸出 −

+------------------+
| format(Amount,2) |
+------------------+
| 60.00            |
| 2,355.40         |
| 456.00           |
| 8,769.00         |
+------------------+
4 rows in set (0.00 sec)

更新於: 2019 年 11 月 11 日

186 次瀏覽

開啟你的 職業 生涯

透過完成課程獲取認證

開始
廣告