1 This article introduces the use of the Group_concat function in MySQL, such as select Group_concat (name). 2 group_concat function in MySQL3 the complete syntax is as follows:4Group_concat ([DISTINCT] field to connect [order by Asc/desc sort field] [Separator'Separators']) 5 Basic Query6Mysql>Select* fromAA;7+------+------+8| id| name |9+------+------+Ten|1|Ten| One|1| -| A|1| -| -|2| -| -|3| $| the|3| -| -+------+------+ - 6Rowsinch Set(0.00sec) - GROUP by ID, print the value of the Name field on one line, comma separated (default) +Mysql>SelectId,group_concat (name) fromAA Group by ID; -+------+--------------------+ +| id| Group_concat (name) | A+------+--------------------+ at|1|Ten, -, -| -|2| -| -|3| $, -| -+------+--------------------+ - 3Rowsinch Set(0.00sec) - GROUP by ID, print the value of the Name field on one line, semicolon delimited inMysql>SelectId,group_concat (Name Separator';') fromAA Group by ID; -+------+----------------------------------+ to| id| Group_concat (Name Separator';') | ++------+----------------------------------+ -|1|Ten; -; -| the|2| -| *|3| $; -| $+------+----------------------------------+Panax Notoginseng 3Rowsinch Set(0.00sec) - GROUP by ID, print the value of the redundant Name field to a line, the comma Delimited +Mysql>SelectId,group_concat (distinct name) fromAA Group by ID; A+------+-----------------------------+ the| id| Group_concat (distinct name) | ++------+-----------------------------+ -|1|Ten, -| $|2| -| $|3| $, -| -+------+-----------------------------+ - 3Rowsinch Set(0.00sec) the GROUP by ID, print the value of the Name field in one line, comma separated, and reverse by name -Mysql>SelectId,group_concat (name order BY name Desc) fromAA Group by ID;Wuyi+------+---------------------------------------+ the| id| Group_concat (name order BY name Desc) | -+------+---------------------------------------+ Wu|1| -, -,Ten| -|2| -| About|3| -, $| $+------+---------------------------------------+ - 3Rowsinch Set(0.00sec) - using the Group_concat_max_len system variable, you can set the maximum allowable length. The syntax for doing this in a program is as follows, where Val is an unsigned integer: -SET [SESSION | GLOBAL] Group_concat_max_len =Val; A if the maximum length has been set, the result is up to this maximum length. +Increase the environment variable Group_concat_max_len. The default is 1024. I set the session-level environment variable to 2048 (not enough to increase)
The use of the Group_concat function in MySQL