使用MySQL中的AVG函数求平均值的教程
MySQL的AVG函数是用来求出各种记录中的字段的平均值。
要了解AVG功能考虑EMPLOYEE_TBL表具有以下记录:
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
现在,假设根据上面的表,要计算所有的dialy_typing_pages的平均值,那么可以通过使用下面的命令:
mysql> SELECT AVG(daily_typing_pages) -> FROM employee_tbl; +-------------------------+ | AVG(daily_typing_pages) | +-------------------------+ | 230.0000 | +-------------------------+ 1 row in set (0.03 sec)
可以把平均使用GROUP BY子句设置的各种记录。下面的例子将采取平均到一个人的所有相关的记录,每个人的平均页打印纸。
mysql> SELECT name, AVG(daily_typing_pages) -> FROM employee_tbl GROUP BY name; +------+-------------------------+ | name | AVG(daily_typing_pages) | +------+-------------------------+ | Jack | 135.0000 | | Jill | 220.0000 | | John | 250.0000 | | Ram | 220.0000 | | Zara | 325.0000 | +------+-------------------------+ 5 rows in set (0.20 sec)
相关推荐
yishujixiaoxiao 2020-06-16
Lius 2020-05-11
achiverhai 2020-01-23
sunnyxuebuhui 2020-01-16
tanrong 2019-12-28
qingmoucsdn 2019-06-28
xiaobeicug 2016-10-06
huangbaokang 2016-02-26
zhengxm 2015-12-11
Oraclezcy 2012-02-21
凤朝飞 2019-04-11
无人机中的城堡 2017-11-30
数据漫谈 2017-11-30
网络游戏 2017-05-16