The complete syntax is as follows:
Group_concat ([DISTINCT] field to connect [order by Asc/desc sort field] [Separator ' delimiter '])
Basic Query
SQL code
- SELECT * from AA;
+------+------+
| id| name |
+------+------+
| 10|
| 20|
| 20|
| 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in Set (0.00 sec)
Group by ID, print the value of the Name field on one line, comma separated (default)
SQL code
- Select Id,group_concat (name) from the AA GROUP by ID;
+------+--------------------+
| id| Group_concat (name) |
+------+--------------------+
| 10,20,20|
| 20 |
|3 | 200,500|
+------+--------------------+
3 Rows in Set (0.00 sec)
Group by ID, print the value of the Name field on one line, semicolon delimited
Java code
- Select Id,group_concat (name separator '; ') from the AA group by ID;
+------+----------------------------------+
| id| Group_concat (name separator '; ') |
+------+----------------------------------+
| 10;20;20 |
| 20|
|3 | 200;500 |
+------+----------------------------------+
3 Rows in Set (0.00 sec)
Group by ID, print the value of the redundant Name field to a line,
Comma delimited
SQL code
- Select Id,group_concat (distinct name) from the AA GROUP by ID;
+------+-----------------------------+
| id| Group_concat (distinct name) |
+------+-----------------------------+
| 10,20|
| 20 |
|3 | 200,500 |
+------+-----------------------------+
3 Rows in Set (0.00 sec)
Group by ID, print the value of the Name field in one line, comma separated, and reverse by name
SQL code
- Select Id,group_concat (name order by name desc) from the AA GROUP by ID;
+------+---------------------------------------+
| id| Group_concat (name order BY name Desc) |
+------+---------------------------------------+
| 20,20,10 |
| 20|
|3 | 500,200|
+------+---------------------------------------+
3 Rows in Set (0.00 sec)
Test SQL, used in the project.
SQL code
- SELECT
- EMPLOYEES. EMPID
- , EMPLOYEES. EmpName
- , departments. Departmentname
- , EMPLOYEES. DeptID
- , EMPLOYEES. Emppwd
- , EMPLOYEES. Insideemail
- , EMPLOYEES. Outsideemail
- , EMPLOYEES. Deleflag
- , EMPLOYEES. Empclass
- , (CONCAT (' [', <span style="color: #ff0000;" >GROUP_CONCAT</span>
- (ROLE. Role_name SEPARATOR '],['), '] ') as ROLENAME
- , (Concat ( ' ['), (
- SELECT
- <span style="color: #ff0000;" >GROUP_CONCAT</span>
- (Departments. Departmentname separator '],[')
- from
- Emp_role_dept
- left JOIN departments
- On (
- Departments. DepartmentID = emp_role_dept. DeptID
- and departments. Deleflag = 0
- )
- GROUP by
- Emp_role_dept. EMPID
- having
- Emp_role_dept. EMPID = EMPLOYEES. EMPID
- ),'] ') as departmentright
- from
- EMPLOYEES
- left JOIN departments
- On (
- Departments. DepartmentID = EMPLOYEES. DeptID
- and departments. Deleflag = 0
- )
- left JOIN role_emp
- On (role_emp. emp_id = EMPLOYEES. EMPID)
- left JOIN ROLE
- On (role_emp. role_id = ROLE. ROLE_ID)
- <span style="color: #ff0000;" > GROUP by
- EMPLOYEES. Empid</span>
- having
- EMPLOYEES. EMPID like '% '
- and EMPLOYEES. EmpName like '% '
- and EMPLOYEES. Deleflag = 0
- and (
- EMPLOYEES. Empclass = ' 1 '
- OR EMPLOYEES. Empclass = ' 2 '
- )
- and EMPLOYEES. DeptID = ' 001 ' LIMIT 0
- , 16
Group_concat function in MySQL