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 uid,year; +-----+------+-------------+------+ | 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 (编辑:PHP编程网 - 黄冈站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|