avg() 是 MySQL 的数学函数。此函数用于从给定表达式中获取平均值。
语法
select avg(aggregate_expression) from table_name [where condition];
参数
aggregate_expression:取平均值的列名
table_name:表名。
where condition:可选。where条件。
返回
此函数返回给定表达式的平均值。
示例 1
select avg(age) as "age" from user;
输出:
mysql> select avg(age) as "age" from user;
+---------+
| age |
+---------+
| 23.5000 |
+---------+
1 row in set (0.02 sec)
示例 2
select avg(age) as "age" from user where id>1;
输出:
mysql> select avg(age) as "age" from user where id>1;
+---------+
| age |
+---------+
| 24.0000 |
+---------+
1 row in set (0.00 sec)