使用 MySQL 處理帶有數字的記錄
要對數字進行四捨五入,請使用 MySQL ROUND()。我們首先建立一個表 -
mysql> create table DemoTable -> ( -> Amount DECIMAL(10,4) -> ); Query OK, 0 rows affected (1.18 sec)
使用 insert 命令在表中插入一些記錄,如下所示 -
mysql> insert into DemoTable values(100.578); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1000.458); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(980.89); Query OK, 1 row affected (0.13 sec)
使用 select 語句從表中顯示所有記錄,如下所示 -
mysql> select * from DemoTable;
這將產生以下輸出 -
+-----------+ | Amount | +-----------+ | 100.5780 | | 1000.4580 | | 980.8900 | +-----------+ 3 rows in set (0.00 sec)
下面是對數字進行四捨五入的查詢 -
mysql> update DemoTable set Amount=round(Amount); Query OK, 3 rows affected (0.12 sec) Rows matched: 3 Changed: 3 Warnings: 0
讓我們再次檢查表記錄 -
mysql> select * from DemoTable;
這將產生以下輸出 -
+-----------+ | Amount | +-----------+ | 101.0000 | | 1000.0000 | | 981.0000 | +-----------+ 3 rows in set (0.00 sec)
廣告