四舍五入函数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;