mysql语句group by用法详解

原文地址:https://www.cnblogs.com/snsdzjlz320/p/5738226.html

 

group by

(1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
(2) group by可用于单个字段分组,也可用于多个字段分组

复制代码
select * from employee;
+------+------+--------+------+------+-------------+
| num  | d_id | name   | age  | sex  | homeaddr    |
+------+------+--------+------+------+-------------+
|    1 | 1001 | 张三   |   26 | 男   | beijinghdq  |
|    2 | 1002 | 李四   |   24 | 女   | beijingcpq  |
|    3 | 1003 | 王五   |   25 | 男   | changshaylq |
|    4 | 1004 | Aric   |   15 | 男   | England     |
+------+------+--------+------+------+-------------+

select * from employee group by d_id,sex;
select * from employee group by sex; +------+------+--------+------+------+------------+ | num | d_id | name | age | sex | homeaddr | +------+------+--------+------+------+------------+ | 2 | 1002 | 李四 | 24 | 女 | beijingcpq | | 1 | 1001 | 张三 | 26 | 男 | beijinghdq | +------+------+--------+------+------+------------+ 根据sex字段来分组,sex字段的全部值只有两个('男'和'女'),所以分为了两组 当group by单独使用时,只显示出每组的第一条记录 所以group by单独使用时的实际意义不大
复制代码

 

group by + group_concat()

(1) group_concat(字段名)可以作为一个输出字段来使用,
(2) 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合

复制代码
select sex from employee group by sex;
+------+
| sex  |
+------+
| 女   |
| 男   |
+------+

select sex,group_concat(name) from employee group by sex;
+–----±-------------------+
| sex | group_concat(name) |
+–----±-------------------+
| 女 | 李四 |
| 男 | 张三,王五,Aric |
+–----±-------------------+

select sex,group_concat(d_id) from employee group by sex;
+–----±-------------------+
| sex | group_concat(d_id) |
+–----±-------------------+
| 女 | 1002 |
| 男 | 1001,1003,1004 |
+–----±-------------------+

复制代码

 

group by + 集合函数

(1) 通过group_concat()的启发,我们既然可以统计出每个分组的某字段的值的集合,那么我们也可以通过集合函数来对这个"值的集合"做一些操作

复制代码
select sex,group_concat(age) from employee group by sex;
+------+-------------------+
| sex  | group_concat(age) |
+------+-------------------+
| 女   | 24                |
| 男   | 26,25,15          |
+------+-------------------+

分别统计性别为男/女的人年龄平均值 select sex,avg(age) from employee group by sex; +------+----------+ | sex | avg(age) | +------+----------+ | 女 | 24.0000 | | 男 | 22.0000 | +------+----------+
分别统计性别为男/女的人的个数 select sex,count(sex) from employee group by sex; +------+------------+ | sex | count(sex) | +------+------------+ | 女 | 1 | | 男 | 3 | +------+------------+
复制代码

 

group by + having

(1) having 条件表达式:用来分组查询后指定一些条件来输出查询结果
(2) having作用和where一样,但having只能用于group by

复制代码
select sex,count(sex) from employee group by sex having count(sex)>2;
+------+------------+
| sex  | count(sex) |
+------+------------+
| 男   |          3 |
+------+------------+
复制代码

 

group by + with rollup

(1) with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和

复制代码
select sex,count(age) from employee group by sex with rollup;
+------+------------+
| sex  | count(age) |
+------+------------+
| 女   |          1 |
| 男   |          3 |
| NULL |          4 |
+------+------------+

select sex,group_concat(age) from employee group by sex with rollup;
+–----±------------------+
| sex | group_concat(age) |
+–----±------------------+
| 女 | 24 |
| 男 | 26,25,15 |
| NULL | 24,26,25,15 |
+–----±------------------+

复制代码

 



 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MySQL中,使用GROUP BY子句可以根据指定的列对查询结果进行分组,并使用聚合函数对每个分组进行计数。 例如,可以使用COUNT()函数来计算每个分组中的记录数量。在引用中的查询语句中,使用了COUNT()函数来计算了不同类型的呼叫数量,包括总呼叫数量、呼入数量和呼出数量等等。 另外,在引用中的查询语句中,也使用了COUNT()函数来计算了不同情况下的呼叫数量,如有通话且呼入的数量、有通话但呼出的数量等等。 总结起来,使用GROUP BY和COUNT()函数可以实现对查询结果的分组计数,从而得到更加详细的统计信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mysql中count(), group by, order by使用详解](https://download.csdn.net/download/weixin_38622227/12827358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [mysql group by count case when](https://blog.csdn.net/zsj777/article/details/80973825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值