The GROUP_CONCAT function concatenates multiple rows of results in the result set into a string.
GROUP_CONCAT([DISTINCT] <str>[, <sep>] [ORDER BY { <col_name> | <expr>} [ASC | DESC]])
| 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. |
Returns a value of type VARCHAR. If the input data contains NULL, returns NULL.
-- setup create table test( value varchar(10) ) distributed by hash(value) buckets 1 properties ("replication_num"="1"); insert into test values ("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 ORDER BY value DESC) from test;
+-----------------------+ | GROUP_CONCAT(`value`) | +-----------------------+ | c, c, b, a | +-----------------------+
select GROUP_CONCAT(DISTINCT value ORDER BY value DESC) from test;
+-----------------------+ | GROUP_CONCAT(`value`) | +-----------------------+ | c, b, a | +-----------------------+
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 | +----------------------------+