计算列值的平均值,并在 MySQL 中不显示小数
为此,你可以将 round() 与 avg() 结合使用。我们先创建一个表 -
mysql> create table DemoTable ( Score int ); Query OK, 0 rows affected (0.68 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(97); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(91); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(86); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.12 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+-------+ | Score | +-------+ | 98 | | 97 | | 91 | | 86 | | 45 | +-------+ 5 rows in set (0.00 sec)
以下是计算列值平均值并显示结果但不显示小数的查询 -
mysql> select round(avg(Score),0) AS Average from DemoTable;
这将产生以下输出 -
+---------+ | Average | +---------+ | 83 | +---------+ 1 row in set (0.00 sec)
广告