mysql中group by 和 group_concat()的区别

1,group_concat()

#例如以下sql查询语句

    root:dmz> select zone_id,group_concat(uin) from fd_sendprize_log group by zone_id;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------+
| zone_id | group_concat(uin)                                                                                                                               
                                                                                                                                                         |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------+
|       2 | 24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,
24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654,24456654 |
|       3 | 325223,325223,325223,325223,325223,325223,325223,325223,325223,325223,325223                                                                    
                                                                                                                                                         |
|       6 | 878333223,878223,878223,878223,878223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878223
,878223,878223,878223,878223,878223,878223                                                                                                               |
|     101 | 878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,878333223,2445223,2445223,2445223,2445223,2445223,2445
223,2445223,2445223,2445223,2445223,2445223,878333223                                                                                                    |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2,group by

#例如以下sql查询语句

  root:dmz> select zone_id,uin from fd_sendprize_log group by zone_id;               
+---------+----------+
| zone_id | uin      |
+---------+----------+
|       2 | 24456654 |
|       3 | 325223   |
|       6 | 878223   |
|     101 | 2445223  |
+---------+----------+

总结:以上可以看出group_concat()是可以查询某个队列下的全部信息   并且默认是,逗号分隔的

           而group by所查询出只是随机的一条分组信息


3,除此之外,还可以对这个组的值来进行排序再连接成字符串,例如按courses_id降序排:

   select stu_id,group_concat(stu_id order by stu_id desc) as courses from stu_table where stu_id=2 group by stu_id;

------------------------------------------

| stu_id        |     courses     |

------------------------------------------

|    2           |       5,4,3        |

------------------------------------------


4,需要注意的是,当连起来的是 int 型,一定要转换成 char 再拼起来

5,长度陷阱,可以使用 group_concat_max_len = val  设置最大的长度

在SQLyog中执行SET GLOBAL  group_concat_max_len = val ,再重新打开生效


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值