MySQL全面瓦解10:分组查询和聚合函数

概述

相信我们经常会遇到这样的场景:想要了解双十一天猫购买化妆品的人员中平均消费额度是多少(这可能有利于对商品价格区间的定位);或者不同年龄段的化妆品消费占比是多少(这可能有助于对商品备货量的预估)。

这个时候就要用到分组查询,分组查询的目的是为了把数据分成多个逻辑组(购买化妆品的人员是一个组,不同年龄段购买化妆品的人员也是组),并对每个组进行聚合计算的过程:。

分组查询的语法格式如下:

1 select cname, group_fun,... from tname [where condition]
2 group by group_expression [having group_condition]; 

说明一下:

1、group_fun 代表聚合函数,是指对分组的数据进行聚合计算的函数。

2、group_expression 代表分组表达式,允许多个,多个之间使用逗号隔开。

3、group_condition 分组之后,再对分组后的数据进行条件过滤的过程。

4、分组语法中,select后面出现的字段 要么是group by后面的字段,要么是聚合函数的列,其他类型会报异常,我们下面的内容中会详细说明。 

说分组之前,先来看看聚合函数,聚合函数是分组查询语法格式中重要的一部分。我们经常需要汇总数据而不用把它们实际检索出来,所以MySQL供了专门的函数。使用这些函数,可用于计算我们需要的数据,以便分析和生成报表。

聚合函数

聚合函数有以下几种。 

函数 说明
AVG() 返回指定字段的平均值
COUNT() 返回查询结果行数
MAX() 返回指定字段的最大值 
MIN() 返回指定字段的最小值
SUM() 返回指定字段的求和值

AVG()函数

AVG()通过对表中行数计数并计算特定列值之和,求得该列的平均值。 AVG()可用来返回所有列的平均值,也可以用来返回特定列或行的平均值。

下面示例返回用户表中用户的平均年龄:

 1 mysql> select * from user2;
 2 +----+--------+------+----------+-----+
 3 | id | name   | age  | address  | sex |
 4 +----+--------+------+----------+-----+
 5 |  1 | brand  |   21 | fuzhou   |   1 |
 6 |  2 | helen  |   20 | quanzhou |   0 |
 7 |  3 | sol    |   21 | xiamen   |   0 |
 8 |  4 | weng   |   33 | guizhou  |   1 |
 9 |  5 | selina |   25 | NULL     |   0 |
10 |  6 | anny   |   23 | shanghai |   0 |
11 |  7 | annd   |   24 | shanghai |   1 |
12 |  8 | sunny  | NULL | guizhou  |   0 |
13 +----+--------+------+----------+-----+
14 8 rows in set
15 
16 mysql> select avg(age) from user2;
17 +----------+
18 | avg(age) |
19 +----------+
20 | 23.8571  |
21 +----------+
22 1 row in set 

注意点:

1、AVG()只能用来确定特定数值列的平均值 。
2、AVG()函数忽略列值为NULL的行,所以上图中age值累加之后是除以7,而不是除以8。 

 

COUNT()函数

COUNT()函数进行计数。 可以用COUNT()确定表中符合条件的行的数目。

count 有 count(*)、count(具体字段)、count(常量) 三种方式来体现 下面 演示了count(*) 和 count(cname)的用法。

 1 mysql> select * from user2;
 2 +----+--------+------+----------+-----+
 3 | id | name   | age  | address  | sex |
 4 +----+--------+------+----------+-----+
 5 |  1 | brand  |   21 | fuzhou   |   1 |
 6 |  2 | helen  |   20 | quanzhou |   0 |
 7 |  3 | sol    |   21 | xiamen   |   0 |
 8 |  4 | weng   |   33 | guizhou  |   1 |
 9 |  5 | selina |   25 | NULL     |   0 |
10 |  6 | anny   |   23 | shanghai |   0 |
11 |  7 | annd   |   24 | shanghai |   1 |
12 |  8 | sunny  | NULL | guizhou  |   0 |
13 +----+--------+------+----------+-----+
14 8 rows in set
15 
16 mysql> select count(*) from user2 where sex=0;
17 +----------+
18 | count(*) |
19 +----------+
20 |        5 |
21 +----------+
22 1 row in set
23 
24 mysql> select count(age) from user2 where sex=0;
25 +------------+
26 | count(age) |
27 +------------+
28 |          4 |
29 +------------+
30 1 row in set 

可以看到,都是取出女生的用户数量,count(*) 比 count(age) 多一个,那是因为age中包含null值。

所以:如果指定列名,则指定列的值为空的行被COUNT()函数忽略,但如果COUNT()函数中用的是星号( *),则不忽略。 

关于count 可以看我写的另一篇,详细分析了几种count的使用和性能比较: SELECT COUNT 小结

MAX()和MIN()函数

MAX()返回指定列中的最大值,MIN()返回指定列中的最小值

 1 mysql> select * from user2;
 2 +----+--------+------+----------+-----+
 3 | id | name   | age  | address  | sex |
 4 +----+--------+------+----------+-----+
 5 |  1 | brand  |   21 | fuzhou   |   1 |
 6 |  2 | helen  |   20 | quanzhou |   0 |
 7 |  3 | sol    |   21 | xiamen   |   0 |
 8 |  4 | weng   |   33 | guizhou  |   1 |
 9 |  5 | selina |   25 | NULL     |   0 |
10 |  6 | anny   |   23 | shanghai |   0 |
11 |  7 | annd   |   24 | shanghai |   1 |
12 |  8 | sunny  | NULL | guizhou  |   0 |
13 +----+--------+------+----------+-----+
14 8 rows in set
15 
16 mysql> select max(age),min(age) from user2;
17 +----------+----------+
18 | max(age) | min(age) |
19 +----------+----------+
20 |       33 |       20 |
21 +----------+----------+
22 1 row in set 

 注意:同样的,MAX()、MIN()函数忽略列值为NULL的行。

SUM函数

SUM()用来返回指定列值的和(总计) ,下面返回了所有年龄的总和,同样的,忽略了null的值

 1 mysql> select * from user2;
 2 +----+--------+------+----------+-----+
 3 | id | name   | age  | address  | sex |
 4 +----+--------+------+----------+-----+
 5 |  1 | brand  |   21 | fuzhou   |   1 |
 6 |  2 | helen  |   20 | quanzhou |   0 |
 7 |  3 | sol    |   21 | xiamen   |   0 |
 8 |  4 | weng   |   33 | guizhou  |   1 |
 9 |  5 | selina |   25 | NULL     |   0 |
10 |  6 | anny   |   23 | shanghai |   0 |
11 |  7 | annd   |   24 | shanghai |   1 |
12 |  8 | sunny  | NULL | guizhou  |   0 |
13 +----+--------+------+----------+-----+
14 8 rows in set
15 
16 mysql> select sum(age) from user2;
17 +----------+
18 | sum(age) |
19 +----------+
20 | 167      |
21 +----------+
22 1 row in set

分组查询

数据准备,假设我们有一个订货单表如下(记载用户的订单金额和下单时间):

 1 mysql> select * from t_order;
 2 +---------+-----+-------+--------+---------------------+------+
 3 | orderid | uid | uname | amount | time                | year |
 4 +---------+-----+-------+--------+---------------------+------+
 5 |      20 |   1 | brand | 91.23  | 2018-08-20 17:22:21 | 2018 |
 6 |      21 |   1 | brand | 87.54  | 2019-07-16 09:21:30 | 2019 |
 7 |      22 |   1 | brand | 166.88 | 2019-04-04 12:23:55 | 2019 |
 8 |      23 |   2 | helyn | 93.73  | 2019-09-15 10:11:11 | 2019 |
 9 |      24 |   2 | helyn | 102.32 | 2019-01-08 17:33:25 | 2019 |
10 |      25 |   2 | helyn | 106.06 | 2019-12-24 12:25:25 | 2019 |
11 |      26 |   2 | helyn | 73.42  | 2020-04-03 17:16:23 | 2020 |
12 |      27 |   3 | sol   | 55.55  | 2019-08-05 19:16:23 | 2019 |
13 |      28 |   3 | sol   | 69.96  | 2020-09-16 19:23:16 | 2020 |
14 |      29 |   4 | weng  | 199.99 | 2020-06-08 19:55:06 | 2020 |
15 +---------+-----+-------+--------+---------------------+------+
16 10 rows in set 

单字段分组

即对于某个字段进行分组,比如针对用户进行分组,输出他们的用户Id,订单数量和总额:

 1 mysql> select uid,count(uid),sum(amount) from t_order group by uid;
 2 +-----+------------+-------------+
 3 | uid | count(uid) | sum(amount) |
 4 +-----+------------+-------------+
 5 |   1 |          3 | 345.65      |
 6 |   2 |          4 | 375.53      |
 7 |   3 |          2 | 125.51      |
 8 |   4 |          1 | 199.99      |
 9 +-----+------------+-------------+
10 4 rows in set 

多字段分组

即对于多个字段进行分组,比如针对用户进行分组,再对他们不同年份的订单数据进行分组,输出订单数量和消费总额:

 1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order group by uid,year;
 2 +-----+------+-------------+------+
 3 | uid | nums | totalamount | year |
 4 +-----+------+-------------+------+
 5 |   1 |    1 | 91.23       | 2018 |
 6 |   1 |    2 | 254.42      | 2019 |
 7 |   2 |    3 | 302.11      | 2019 |
 8 |   2 |    1 | 73.42       | 2020 |
 9 |   3 |    1 | 55.55       | 2019 |
10 |   3 |    1 | 69.96       | 2020 |
11 |   4 |    1 | 199.99      | 2020 |
12 +-----+------+-------------+------+
13 7 rows in set 

分组前的条件过滤:where

这个很简单,就是再分组(group by)之前通过where关键字进行条件过滤,取出我们需要的数据,假设我们只要列出2019年8月之后的数据,源数据只有6条合格的,有两条年份一样被分组的:

 1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order where time > '2019-08-01' group by uid,year;
 2 +-----+------+-------------+------+
 3 | uid | nums | totalamount | year |
 4 +-----+------+-------------+------+
 5 |   2 |    2 | 199.79      | 2019 |
 6 |   2 |    1 | 73.42       | 2020 |
 7 |   3 |    1 | 55.55       | 2019 |
 8 |   3 |    1 | 69.96       | 2020 |
 9 |   4 |    1 | 199.99      | 2020 |
10 +-----+------+-------------+------+
11 5 rows in set 

分组后的条件过滤:having

有时候我们需要再分组之后再对数据进行过滤,这时候就需要使用having关键字进行数据过滤,再上述条件下,我们需要取出消费次数超过一次的数据:

1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount,year from t_order where time > '2019-08-01' group by uid,year having nums>1;
2 +-----+------+-------------+------+
3 | uid | nums | totalamount | year |
4 +-----+------+-------------+------+
5 |   2 |    2 | 199.79      | 2019 |
6 +-----+------+-------------+------+
7 1 row in set 

这边需要注意区分where和having:

where是在分组(聚合)前对记录进行筛选,而having是在分组结束后的结果里筛选,最后返回过滤后的结果。

可以把having理解为两级查询,即含having的查询操作先获得不含having子句时的sql查询结果表,然后在这个结果表上使用having条件筛选出符合的记录,最后返回这些记录,因此,having后是可以跟聚合函数的,并且这个聚集函数不必与select后面的聚集函数相同。

分组后的排序处理

order条件接在group by后面,也就是统计出每个用户的消费总额和消费次数后,对用户的消费总额进行降序排序的过程。

 1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid;
 2 +-----+------+-------------+
 3 | uid | nums | totalamount |
 4 +-----+------+-------------+
 5 |   1 |    3 | 345.65      |
 6 |   2 |    4 | 375.53      |
 7 |   3 |    2 | 125.51      |
 8 |   4 |    1 | 199.99      |
 9 +-----+------+-------------+
10 4 rows in set
11 
12 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid order by totalamount desc;
13 +-----+------+-------------+
14 | uid | nums | totalamount |
15 +-----+------+-------------+
16 |   2 |    4 | 375.53      |
17 |   1 |    3 | 345.65      |
18 |   4 |    1 | 199.99      |
19 |   3 |    2 | 125.51      |
20 +-----+------+-------------+
21 4 rows in set 

分组后的limit 限制

limit限制关键字一般放在语句的最末尾,比如基于我们上面的搜索,我们再limit 1,只取出消费额最高的那条,其他跳过。

1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid order by totalamount desc limit 1;
2 +-----+------+-------------+
3 | uid | nums | totalamount |
4 +-----+------+-------------+
5 |   2 |    4 | 375.53      |
6 +-----+------+-------------+
7 1 row in set 

关键字的执行顺序

我们看到上面那我们用了 where、group by、having、order by、limit这些关键字,如果一起使用,他们是有先后顺序,顺序错了会导致异常,语法格式如下:

1 select cname from tname
2 where [原表查询条件]
3 group by [分组表达式]
4 having [分组过滤条件]
5 order by [排序条件]
6 limit [offset,] count;

 

1 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order where time > '2019-08-01' group by uid having totalamount>100 order by totalamount desc limit 1;
2 +-----+------+-------------+
3 | uid | nums | totalamount |
4 +-----+------+-------------+
5 |   2 |    3 | 273.21      |
6 +-----+------+-------------+
7 1 row in set

 

总结

1、分组语法中,select后面出现的字段 要么是group by后面的字段,要么是聚合函数的列,其他类型会报异常:可以自己试试。

2、分组关键字的执行顺序:where、group by、having、order by、limit,顺序不能调换,否则会报异常:可以自己试试。

posted @ 2020-11-16 08:30  Hello-Brand  阅读(1273)  评论(0编辑  收藏  举报