0
点赞
收藏
分享

微信扫一扫

group_concat函数详解


MySQL中group_concat函数

完整的语法如下:

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

 

基本查询



  1. select * from


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(namefrom aa group by


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;  


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 namefrom aa group by


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 descfrom aa group by


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. '[', <SPAN style="COLOR: #ff0000">GROUP_CONCAT</SPAN>   
  12. (ROLE.Role_Name SEPARATOR '],['), ']')) AS
  13. '[', (   
  14. SELECT
  15. "COLOR: #ff0000">GROUP_CONCAT</SPAN>   
  16. (DEPARTMENTS.DEPARTMENTNAME separator '],[')   
  17. FROM
  18.                     EMP_ROLE_DEPT   
  19. LEFT JOIN
  20. ON
  21.                                 DEPARTMENTS.DEPARTMENTID = EMP_ROLE_DEPT.DEPTID   
  22. AND
  23.                             )   
  24. GROUP BY
  25.                     EMP_ROLE_DEPT.EMPID   
  26. HAVING
  27.                     EMP_ROLE_DEPT.EMPID = EMPLOYEES.EMPID   
  28. ']')) AS
  29. FROM
  30.         EMPLOYEES   
  31. LEFT JOIN
  32. ON
  33.                     DEPARTMENTS.DEPARTMENTID = EMPLOYEES.DEPTID   
  34. AND
  35.                 )   
  36. LEFT JOIN
  37. ON
  38. LEFT JOIN
  39. ON
  40. <SPAN style="COLOR: #ff0000">    GROUP BY
  41.         EMPLOYEES.EMPID</SPAN>   
  42.   
  43. HAVING
  44. LIKE '%%'
  45. AND EMPLOYEES.EMPNAME LIKE '%%'
  46. AND
  47. AND
  48. '1'
  49. OR EMPLOYEES.EMPCLASS = '2'
  50.         )   
  51. AND EMPLOYEES.DEPTID = '001'
  52.         ,16   
举报

相关推荐

0 条评论