MySQL基础系列:数据统计sum,count

1 数据表数据

MySQL基础系列:数据统计sum,count_第1张图片

图1 数据表数据

2 sum

  • 功能
    数据求和统计.
序号 表达式 描述
1 sum(column_name) 所有列的列值相加
2 sum(expression) 统计满足表达式的所有行

2.1 sum(column_name)

  • 查询语句:
select sum(id) from userinfos group by sex;
  • 查询结果:
+---------+
| sum(id) |
+---------+
|     166 |
|      36 |
+---------+

2.2 sum(experssion)

  • 查询语句:
select sum(position="CEO") from userinfos group by sex;
  • 查询结果:
+---------------------+
| sum(position="CEO") |
+---------------------+
|                   4 |
|                   1 |
+---------------------+

3 count

  • 功能
    数据数量统计.

3.1 数据形式

  • 不存在NULL:

MySQL基础系列:数据统计sum,count_第2张图片

图2 表数据不存在NULL

  • 存在NULL:
    MySQL基础系列:数据统计sum,count_第3张图片
图3 表数据存在NULL

3.2 count功能分类

序号 表达式 描述
1 count(*) 统计所有行,包括NULL
2 count(column_name) 统计列不为NULL的行数
3 count(expression) 只要非NULL即+1,与条件表达式(expression)无关

3.2.1 count(*)

  • 查询语句:
select count(*) from userinfos group by sex;
  • 查询结果:
+----------+
| count(*) |
+----------+
|       11 |
|        5 |
+----------+

3.2.2 count(column_name)

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 |
+-----------------+

3.2.3 count(expression)

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 |
+--------+-----------------------+

4 found_rows

  • 功能
    获取分组数据的行数.

4.1 一次查询

  • 查询语句:
select found_rows() from userinfos group by sex;
  • 查询结果:
+--------------+
| found_rows() |
+--------------+
|            2 |
|            2 |
+--------------+

返回一个列表数据,两行,每行数据为分组数据的行数,读取一行数据即可,若使用MyBatis,则统计列表的长度,因为MyBatis中返回的数据为1,1.

4.2 两次查询

  • 查询语句:
select sql_calc_found_rows sex from userinfos group by sex;
  • 查询结果:
+--------+
| sex    |
+--------+
| female |
| male   |
+--------+

查询语句:

select found_rows();

查询结果:

+--------------+
| found_rows() |
+--------------+
|            2 |
+--------------+

5 group by

  • 功能
    数据分组,细分两个分类:单个分组字段,;多个分组字段,数据分组.

5.1 单字段分组

选择(select)的对象,只能是分组的字段或进行统计的字段信息(如sum,count)

5.1.0 只获取分组字段数据

  • 查询语句:
select sex from userinfos group by sex;
  • 分组结果:
+--------+
| sex    |
+--------+
| female |
| male   |
+--------+

将sex字段进行分析,相同的内容进行合并,放在同一组中,返回一条数据.

5.1.2 获取分组字段数据和统计信息

  • 查询语句:
select sex, count(*) from userinfos group by sex;
  • 查询结果:
+--------+----------+
| sex    | count(*) |
+--------+----------+
| female |       11 |
| male   |        5 |
+--------+----------+

对sex字段分析,并统计每组的所有数据数量,即female组中共有11个数据,male共有5个数据.

5.1.3 获取统计信息

  • 查询语句:
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个

5.1.4 无效语句

选择(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,该列不在分组中,无法获取查询结果,因此单个字段分组时,选择的字段必须是分组的字段.

5.2 多字段分组

  • 查询语句:
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分组,相同的合并.


你可能感兴趣的:(#,MySQL,MySQL,GroupBy,found_rows,sum,count)