group_concat函数在MySQL中的使用

group_concat函数在MySQL中的使用

作用

将group by产生的同一个分组中的值连接起来,返回一个字符串结果。

语法格式

1
group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator ‘分隔符’] )

示例

  • 查询所有数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    mysql> select * from test_table;
    +----+-----+
    | id | num |
    +----+-----+
    | 1 | 10 |
    | 1 | 20 |
    | 1 | 20 |
    | 2 | 10 |
    | 2 | 20 |
    | 3 | 30 |
    +----+-----+
    6 rows in set
  • 使用group_concat函数分组连接

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> select id,group_concat(num) from test_table group by id;
    +----+-------------------+
    | id | group_concat(num) |
    +----+-------------------+
    | 1 | 10,20,20 |
    | 2 | 10,20 |
    | 3 | 30 |
    +----+-------------------+
    3 rows in set
  • 使用group_concat函数并剔除分组重复项

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> select id,group_concat(distinct num) from test_table group by id;
    * +----+----------------------------+
    | id | group_concat(distinct num) |
    +----+----------------------------+
    | 1 | 10,20 |
    | 2 | 10,20 |
    | 3 | 30 |
    +----+----------------------------+
    3 rows in set
  • 使用group_concat函数并剔除分组重复项根据num字段降序排列

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> select id,group_concat(distinct num order by num desc) from test_table group by id;
    +----+----------------------------------------------+
    | id | group_concat(distinct num order by num desc) |
    +----+----------------------------------------------+
    | 1 | 20,10 |
    | 2 | 20,10 |
    | 3 | 30 |
    +----+----------------------------------------------+
    3 rows in set
  • 注意:这里是根据连接字符串进行升序或降序排列,如果需要根据id排序,在外层使用order by

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> select id,group_concat(distinct num order by num desc) from test_table group by id order by id desc;
    +----+----------------------------------------------+
    | id | group_concat(distinct num order by num desc) |
    +----+----------------------------------------------+
    | 3 | 30 |
    | 2 | 20,10 |
    | 1 | 20,10 |
    +----+----------------------------------------------+
    3 rows in set
  • 使用指定分隔符

    1
    2
    3
    4
    5
    6
    7
    8
    9
    mysql> select id,group_concat(num separator ':') from test_table group by id;
    +----+---------------------------------+
    | id | group_concat(num separator ':') |
    +----+---------------------------------+
    | 1 | 10:20:20 |
    | 2 | 10:20 |
    | 3 | 30 |
    +----+---------------------------------+
    3 rows in set

注意事项

group_concat函数处理字符串的长度是由限制的,默认长度为1024。如果拼接的字符串超过1024,就会在数据库里面被截取,所以不会显示所有拼接数据。有两种方式可以修改最大长度:

  1. 修改配置文件/etc/my.cnf
    1
    2
    [mysqld]
    group_concat_max_len = -1 # -1为最大值或填入你要的最大长度
  2. 使用SQL语句直接设置
    1
    2
    3
    4
    ## 设置当前session的group_concat长度,其他session连接不受影响:
    SET SESSION group_concat_max_len = 102400;
    ## 设置全局group_concat长度:
    SET GLOBAL group_concat_max_len = 102400

参考资料

https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat

打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 henrrywan

请我喝杯咖啡吧~

支付宝
微信