在 MySQL 資料庫中儲存貨幣值時,哪個資料型別最為合適?
為了表示貨幣,我們需要使用 Decimal (TotalDigitsinteger, DigitsAfterDecimalinteger) 方法。
比如,我們需要顯示 345.66 這個值。這當中有幾位數。345.66 中共有 5 位數,小數點後兩位,也就是 66。
我們可以藉助 MySQL 的 Decimal() 方法來表示同一內容。以下是準確表示方法。
DECIMAL(5,2)
讓我們先建立一個表格,並對我們示例中的上述表示法進行考慮 −
mysql> create table MoneyRepresentation -> ( -> Money Decimal(5,2) -> ); Query OK, 0 rows affected (0.65 sec)
讓我們插入相同的值,即 345.66
mysql> insert into MoneyRepresentation values(345.66); Query OK, 1 row affected (0.13 sec)
藉助 SELECT 語句顯示所有記錄。查詢如下 −
mysql> select *from MoneyRepresentation;
以下是輸出結果。
+--------+ | Money | +--------+ | 345.66 | +--------+ 1 row in set (0.00 sec)
檢視上述輸出,我們得到 5 位數總數,並在小數點後添加了 2 位數字,因為我們已將函式設定為
Decimal(5,2)
廣告