GROUP_CONCAT
Descriptionβ
The GROUP_CONCAT function concatenates multiple rows of results in the result set into a string.
Syntaxβ
GROUP_CONCAT([DISTINCT] <str>[, <sep>] [ORDER BY { <col_name> | <expr>} [ASC | DESC]])
Parametersβ
Parameters | Description |
---|---|
<str> | Required. The expression of the value to be concatenated. |
<sep> | Optional. The separator between strings. |
<col_name> | Optional. The column used for sorting. |
<expr> | Optional. The expression used for sorting. |
Return Valueβ
Returns a value of type VARCHAR.
Exampleβ
select value from test;
+-------+
| value |
+-------+
| a |
| b |
| c |
| c |
+-------+
select GROUP_CONCAT(value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c, c |
+-----------------------+
select GROUP_CONCAT(DISTINCT value) from test;
+-----------------------+
| GROUP_CONCAT(`value`) |
+-----------------------+
| a, b, c |
+-----------------------+
select GROUP_CONCAT(value, " ") from test;
+----------------------------+
| GROUP_CONCAT(`value`, ' ') |
+----------------------------+
| a b c c |
+----------------------------+
select GROUP_CONCAT(value, NULL) from test;
+----------------------------+
| GROUP_CONCAT(`value`, NULL)|
+----------------------------+
| NULL |
+----------------------------+