Example of group_concat function usage for mysql

  • 2020-06-15 10:23:33
  • OfStack

The group_concat function in MySQL has the following complete syntax:


group_concat([DISTINCT]  The fields to connect to  [Order BY ASC/DESC  Sort field ] [Separator ' The separator '])

The basic query


select * from aa;


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

Grouped in id, print the value of the name field on line 1, comma separated (default)


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


Related articles: