Mysql中关于 group_concat函数详解

时间:2021-06-28 17:41:08

group_concat()主要功能:能将相同的行组合起来

完整的语法如下:

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

基本查询

  1. select * from aa;

+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔(默认)

  1. select id,group_concat(name) from aa group by id;

+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,分号分隔

  1. select id,group_concat(name separator ';') from aa group by id;

+------+----------------------------------+
| id| group_concat(name separator ';') |
+------+----------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
+------+----------------------------------+
3 rows in set (0.00 sec)

以id分组,把去冗余的name字段的值打印在一行,

逗号分隔

  1. select id,group_concat(distinct name) from aa group by id;

+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)

以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序

  1. select id,group_concat(name order by name desc) from aa group by id;

+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)

测试sql,项目中用到的。

    1. SELECT
    2. EMPLOYEES.EMPID
    3. ,EMPLOYEES.EMPNAME
    4. ,DEPARTMENTS.DEPARTMENTNAME
    5. ,EMPLOYEES.DEPTID
    6. ,EMPLOYEES.EMPPWD
    7. ,EMPLOYEES.INSIDEEMAIL
    8. ,EMPLOYEES.OUTSIDEEMAIL
    9. ,EMPLOYEES.DELEFLAG
    10. ,EMPLOYEES.EMPCLASS
    11. ,(CONCAT('[', <span style="color: #ff0000;">GROUP_CONCAT</span>
    12. (ROLE.Role_Name SEPARATOR '],['), ']')) AS ROLENAME
    13. ,(concat( '[', (
    14. SELECT
    15. <span style="color: #ff0000;">GROUP_CONCAT</span>
    16. (DEPARTMENTS.DEPARTMENTNAME separator '],[')
    17. FROM
    18. EMP_ROLE_DEPT
    19. LEFT JOIN DEPARTMENTS
    20. ON (
    21. DEPARTMENTS.DEPARTMENTID = EMP_ROLE_DEPT.DEPTID
    22. AND DEPARTMENTS.DELEFLAG = 0
    23. )
    24. GROUP BY
    25. EMP_ROLE_DEPT.EMPID
    26. HAVING
    27. EMP_ROLE_DEPT.EMPID = EMPLOYEES.EMPID
    28. ),']')) AS DEPARTMENTRIGHT
    29. FROM
    30. EMPLOYEES
    31. LEFT JOIN DEPARTMENTS
    32. ON (
    33. DEPARTMENTS.DEPARTMENTID = EMPLOYEES.DEPTID
    34. AND DEPARTMENTS.DELEFLAG = 0
    35. )
    36. LEFT JOIN ROLE_EMP
    37. ON (ROLE_EMP.EMP_ID = EMPLOYEES.EMPID)
    38. LEFT JOIN ROLE
    39. ON (ROLE_EMP.ROLE_ID = ROLE.ROLE_ID)
    40. <span style="color: #ff0000;">    GROUP BY
    41. EMPLOYEES.EMPID</span>
    42. HAVING
    43. EMPLOYEES.EMPID LIKE '%%'
    44. AND EMPLOYEES.EMPNAME LIKE '%%'
    45. AND EMPLOYEES.DELEFLAG = 0
    46. AND (
    47. EMPLOYEES.EMPCLASS = '1'
    48. OR EMPLOYEES.EMPCLASS = '2'
    49. )
    50. AND EMPLOYEES.DEPTID = '001' LIMIT 0
    51. ,16

相关文章