四舍五入函數ROUND(x,y)
參數:
x:數據
y:需要保留的小數點位數
ROUND(x,y)函數返回最接近於參數x的數,其值保留到小數點后面y位,若y為負值,則將保留x值到小數點左邊y位。
mysql> select round(32.154,2);
+-----------------+
| round(32.154,2) |
+-----------------+
| 32.15 |
+-----------------+
1 row in set (0.41 sec)
mysql> select round(32.154,1);
+-----------------+
| round(32.154,1) |
+-----------------+
| 32.2 |
+-----------------+
1 row in set (0.02 sec)
mysql> select round(32.154,0);
+-----------------+
| round(32.154,0) |
+-----------------+
| 32 |
+-----------------+
1 row in set (0.00 sec)
mysql> select round(32.154,3);
+-----------------+
| round(32.154,3) |
+-----------------+
| 32.154 |
+-----------------+
1 row in set (0.00 sec)
mysql>
求所有電腦產品的平均價格,並且保留兩位小數,AVG,MAX,MIN、COUNT、SUM為聚合函數
SELECT AVG(goods_price) AS avg_price FROM tdb_goods;
SELECT ROUND(avg_price,2) AS avg_price FROM tdb_goods;