This article introduces the use of the Group_concat function in MySQL, such as select Group_concat (name). The complete syntax for the GROUP_CONCAT function in MySQL is as follows: Group_concat ([DISTINCT] The field to connect [order by Asc/desc sort field] [Separator ' delimiter ']) basic query mysql> SELECT * FROM aa;+------+------+| id| Name |+------+------+|1 | 10| | 1 | 20| | 1 | 20| | 2 | 20| | 3 | 200 | | 3 | |+------+------+6 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field on one line, comma separated (default) mysql> 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) is grouped by ID, printing the value of the Name field on one line, semicolon separating mysql> select Id,group_ Concat (name separator '; ') from the 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) is grouped by ID, printing the value of the de-redundant name field on one line, separated by commas mysql> Select Id,group_concat (Distinct NAMe) from the AA group by id;+------+-----------------------------+| id| Group_concat (distinct name) |+------+-----------------------------+|1 | 10,20| | 2 | 20 | | 3 | 200,500 |+------+-----------------------------+3 rows in Set (0.00 sec) is grouped by ID, printing the value of the Name field in one line, separated by commas, and reversed by name Mysql> Select Id,group_concat (name order BY name Desc) from the 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)
For length setting, please see the group_concat length setting link .
Simple usage:
SET GLOBAL group_concat_max_len=102400, followed by the SQL statement containing the GROUP_CONCAT. (Global query settings, as long as the settings, this MySQL server here the length is permanently set to this value.) )
SET SESSION group_concat_max_len=102400, and then directly with the SQL statement containing the GROUP_CONCAT. (the setting of a session query will only take effect if the query is in the same session)
MySQL Group_concat function---useful one to query all group by groups after all the contents of the same groups