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

爱新觉羅丶高
发布于 2022-8-1 18:43
浏览
1收藏

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

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

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

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

说明一下:

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

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

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

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

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

聚合函数
聚合函数有以下几种。 

MySQL全面瓦解10:分组查询和聚合函数-鸿蒙开发者社区

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

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

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

注意点:

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

 

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

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

 mysql> select * from user2;
 +----+--------+------+----------+-----+
 | id | name   | age  | address  | sex |
 +----+--------+------+----------+-----+
 |  1 | brand  |   21 | fuzhou   |   1 |
 |  2 | helen  |   20 | quanzhou |   0 |
 |  3 | sol    |   21 | xiamen   |   0 |
 |  4 | weng   |   33 | guizhou  |   1 |
 |  5 | selina |   25 | NULL     |   0 |
 |  6 | anny   |   23 | shanghai |   0 |
 |  7 | annd   |   24 | shanghai |   1 |
 |  8 | sunny  | NULL | guizhou  |   0 |
 +----+--------+------+----------+-----+
 8 rows in set
 
 mysql> select count(*) from user2 where sex=0;
 +----------+
 | count(*) |
 +----------+
 |        5 |
 +----------+
 1 row in set
 
 mysql> select count(age) from user2 where sex=0;
 +------------+
 | count(age) |
 +------------+
 |          4 |
 +------------+
 1 row in set 

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

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

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

MAX()和MIN()函数

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

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

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

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

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

分组查询

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

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

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

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

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

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

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

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

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

 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;
 +-----+------+-------------+------+
 | uid | nums | totalamount | year |
 +-----+------+-------------+------+
 |   2 |    2 | 199.79      | 2019 |
 +-----+------+-------------+------+
 1 row in set 

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

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

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

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

 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid;
 +-----+------+-------------+
 | uid | nums | totalamount |
 +-----+------+-------------+
 |   1 |    3 | 345.65      |
 |   2 |    4 | 375.53      |
 |   3 |    2 | 125.51      |
 |   4 |    1 | 199.99      |
 +-----+------+-------------+
 4 rows in set
 
 mysql> select uid,count(uid) as nums,sum(amount) as totalamount from t_order group by uid order by totalamount desc;
 +-----+------+-------------+
 | uid | nums | totalamount |
 +-----+------+-------------+
 |   2 |    4 | 375.53      |
 |   1 |    3 | 345.65      |
 |   4 |    1 | 199.99      |
 |   3 |    2 | 125.51      |
 +-----+------+-------------+
 4 rows in set 

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

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

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

select cname from tname
where [原表查询条件]
group by [分组表达式]
having [分组过滤条件]
order by [排序条件]
limit [offset,] count;
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;
+-----+------+-------------+
| uid | nums | totalamount |
+-----+------+-------------+
|   2 |    3 | 273.21      |
+-----+------+-------------+
1 row in set

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

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

 

 

文章转载自公众号:架构与思维

已于2022-8-1 18:43:42修改
收藏 1
回复
举报
回复
    相关推荐