使用聚合函数查找MySQL中列值的平均值
让我们首先创建一个表-
mysql> create table DemoTable ( Number int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(56); mysql> insert into DemoTable values(78); mysql> insert into DemoTable values(89); mysql> insert into DemoTable values(98); mysql> insert into DemoTable values(91); mysql> insert into DemoTable values(96);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+ | Number | +--------+ | 56 | | 78 | | 89 | | 98 | | 91 | | 96 | +--------+ 6 rows in set (0.00 sec)
以下是查询以获取列值的平均值-
mysql> select (sum(Number)*(100))/(100+500) from DemoTable;
这将产生以下输出-
+-------------------------------+ | (sum(Number)*(100))/(100+500) | +-------------------------------+ | 84.6667 | +-------------------------------+ 1 row in set (0.00 sec)