Mysql分组函数/多行处理函数:输入多行,最终输出一行。count(),sum(),avg(),max(),min()。分组函数必须先进行分组,然后才能用。如果没有对数据分组,整张表默认为一组
1、分组函数使用的注意事项:分组函数自动忽略null,不需要提前对null进行处理
mysql> select sum(salary) as sum from user1;
+-------+
| sum |
+-------+
| 19000 |
+-------+
1 row in set (0.01 sec)
mysql> select count(salary) as count from user1;
+-------+
| count |
+-------+
| 1 |
+-------+
1 row in set (0.00 sec)
2、分组函数中count(*)和count(具体字段)的区别:count(*)不忽略null,count(具体字段)忽略null
mysql> select count(*) from user1;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)
mysql> select count(salary) from user1;
+---------------+
| count(salary) |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
3、分组函数不能直接使用在where子句中
mysql> select user_name from user1 where age>min(age);
ERROR 1111 (HY000): Invalid use of group function
4、所有的分组函数都可以联合起来用
mysql> select avg(age),sum(age),max(age),min(age),count(age) from user1;
+----------+----------+----------+----------+------------+
| avg(age) | sum(age) | max(age) | min(age) | count(age) |
+----------+----------+----------+----------+------------+
| 700.0000 | 2800 | 900 | 100 | 4 |
+----------+----------+----------+----------+------------+
1 row in set (0.01 sec)