mysql中求平均值的函数是avg()函数,使用语法“select avg(字段名) form 表名”,意思为查询某表某字段的平均值;在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)