序号 | 表达式 | 描述 |
---|---|---|
1 | sum(column_name) | 所有列的列值相加 |
2 | sum(expression) | 统计满足表达式的所有行 |
select sum(id) from userinfos group by sex;
+---------+
| sum(id) |
+---------+
| 166 |
| 36 |
+---------+
select sum(position="CEO") from userinfos group by sex;
+---------------------+
| sum(position="CEO") |
+---------------------+
| 4 |
| 1 |
+---------------------+
序号 | 表达式 | 描述 |
---|---|---|
1 | count(*) | 统计所有行,包括NULL |
2 | count(column_name) | 统计列不为NULL的行数 |
3 | count(expression) | 只要非NULL即+1,与条件表达式(expression)无关 |
select count(*) from userinfos group by sex;
+----------+
| count(*) |
+----------+
| 11 |
| 5 |
+----------+
position不存在null值:
select count(position) from userinfos group by sex;
+-----------------+
| count(position) |
+-----------------+
| 11 |
| 5 |
+-----------------+
position存在null值:
select count(position) from userinfos group by sex;
+-----------------+
| count(position) |
+-----------------+
| 10 |
| 5 |
+-----------------+
position不存在null值:
select sex, count(position="CEO") from userinfos group by sex;
+--------+-----------------------+
| sex | count(position="CEO") |
+--------+-----------------------+
| female | 11 |
| male | 5 |
+--------+-----------------------+
position存在null值:
select sex, count(position="CEO") from userinfos group by sex;
+--------+-----------------------+
| sex | count(position="CEO") |
+--------+-----------------------+
| female | 10 |
| male | 5 |
+--------+-----------------------+
select found_rows() from userinfos group by sex;
+--------------+
| found_rows() |
+--------------+
| 2 |
| 2 |
+--------------+
返回一个列表数据,两行,每行数据为分组数据的行数,读取一行数据即可,若使用MyBatis,则统计列表的长度,因为MyBatis中返回的数据为1,1.
select sql_calc_found_rows sex from userinfos group by sex;
+--------+
| sex |
+--------+
| female |
| male |
+--------+
查询语句:
select found_rows();
查询结果:
+--------------+
| found_rows() |
+--------------+
| 2 |
+--------------+
选择(select)的对象,只能是分组的字段或进行统计的字段信息(如sum,count)
select sex from userinfos group by sex;
+--------+
| sex |
+--------+
| female |
| male |
+--------+
将sex字段进行分析,相同的内容进行合并,放在同一组中,返回一条数据.
select sex, count(*) from userinfos group by sex;
+--------+----------+
| sex | count(*) |
+--------+----------+
| female | 11 |
| male | 5 |
+--------+----------+
对sex字段分析,并统计每组的所有数据数量,即female组中共有11个数据,male共有5个数据.
select sex, count(*), sum(position="CEO") from userinfos group by sex;
+--------+----------+---------------------+
| sex | count(*) | sum(position="CEO") |
+--------+----------+---------------------+
| female | 11 | 5 |
| male | 5 | 1 |
+--------+----------+---------------------+
使用sum获取某个字段在分组中的数据,如position="CEO"的数据,female共有5个,male共有1个
选择(select)分组以外的字段,如(position),无法获取数据.
select sex, position from userinfos group by sex;
ERROR 1055 (42000): Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'data_repository.userinfos.position' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
选择的列表(list[sex,position])不在分组语句中(GROUP BY)并且包含了非聚合的列position,该列不在分组中,无法获取查询结果,因此单个字段分组时,选择的字段必须是分组的字段.
mysql> select sex from userinfos group by sex, position;
+--------+
| sex |
+--------+
| female |
| female |
| female |
| female |
| male |
| male |
| male |
| male |
+--------+
对sex和position字段进行过滤,先将sex字段分组,然后将position字段分析,合并查询结果,选择的对象为分组对象的子集.
select sex, position from userinfos group by sex, position;
+--------+----------+
| sex | position |
+--------+----------+
| female | CEO |
| female | CFO |
| female | COO |
| female | CTO |
| male | CEO |
| male | CFO |
| male | COO |
| male | CTO |
+--------+----------+
先依据sex分组,后按照position分组,相同的合并.