使用聚合函数查找 MySQL 中列值的平均值
让我们首先创建一个表 -
mysql> create table DemoTable ( Number int ); Query OK, 0 rows affected (0.79 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(98); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(91); Query OK, 1 row affected (0.35 sec) mysql> insert into DemoTable values(96); Query OK, 1 row affected (0.15 sec)
使用 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)
广告