group_concat
Concatenates non-null values from a group into a single string, with a sep argument, which is , by default if not specified. This function can be used to concatenate values from multiple rows of a column into one string.
group_concat supports DISTINCT and ORDER BY in 3.0 versions later than 3.0.6 and 3.1 versions later than 3.1.3.
Syntaxβ
VARCHAR GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR sep])
Parametersβ
expr: the values to concatenate, with null values ignored. It must evaluate to VARCHAR. You can optionally specifyDISTINCTto eliminate duplicate values from the output string. If you want to concatenate multipleexprdirectly, use concat or concat_ws to specify formats.- Items in ORDER BY can be unsigned integers (starting from 1), column names, or normal expressions. The results are sorted in ascending order by default. You can also explicitly specify the ASC keyword. If you want to sort results in descending order, add the DESC keyword to the name of the column you are sorting.
sep: the optional separator used to concatenate non-null values from different rows. If it is not specified,,(a comma) is used by default. To eliminate the separator, specify an empty string''.
NOTE
From v3.0.6 and v3.1.3 onwards, there is a behavior change when you specify the separator. You must use
SEPARATORto declare the separator, for example,select group_concat(name SEPARATOR '-') as res from ss;.
Return valueβ
Returns a string value for each group and returns NULL if there are no non-NULL values.
You can limit the length of the string returned by group_concat by setting the session variable group_concat_max_len, which defaults to 1024. Minimum value: 4. Unit: characters.
Example:
SET [GLOBAL | SESSION] group_concat_max_len = <value>;