MySQL 中最適合用於貨幣的資料型別是什麼?


MySQL 中最適合用於貨幣的資料型別是 DECIMAL。DECIMAL 資料型別的語法如下 -

DECIMAL(TotalDigit,NumberOfDigitAfterDecimalPoint);

為了理解上述語法,讓我們建立一個表。建立表所需的查詢如下 -

mysql> create table CurrenciesDemo
   -> (
   -> TotalPrice DECIMAL(10,2)
   -> );
Query OK, 0 rows affected (1.82 sec)

使用 insert 命令在表中插入一些記錄。查詢如下 -

mysql> insert into CurrenciesDemo values(1647575.67);
Query OK, 1 row affected (0.19 sec)

mysql> insert into CurrenciesDemo values(1647575);
Query OK, 1 row affected (0.21 sec)

mysql> insert into CurrenciesDemo values(99599596);
Query OK, 1 row affected (0.13 sec)

mysql> insert into CurrenciesDemo values(1986.50);
Query OK, 1 row affected (0.23 sec)

mysql> insert into CurrenciesDemo values(99999999.90);
Query OK, 1 row affected (0.17 sec)

使用 select 語句顯示錶中的所有記錄。查詢如下 -

mysql> select *from CurrenciesDemo;

以下是輸出 -

+-------------+
| TotalPrice  |
+-------------+
| 1647575.67  |
| 1647575.00  |
| 99599596.00 |
| 1986.50     |
| 99999999.90 |
+-------------+
5 rows in set (0.00 sec)

以下是使用字首符號(如美元)顯示上述所有價格的查詢 -

mysql> select concat('$',TotalPrice) as DollerSign from CurrenciesDemo;

以下是輸出 -

+--------------+
| DollerSign   |
+--------------+
| $1647575.67  |
| $1647575.00 |
| $99599596.00 |
| $1986.50 |
| $99999999.90 |
+--------------+
5 rows in set (0.00 sec)

更新日期: 2020-06-30

154 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.