GROUP_CONCAT
Function Description
The GROUP_CONCAT function returns a string that joins content specified by a column or expression.
If the result set does not have any rows, this function returns NULL.
Function Syntax
> GROUP_CONCAT(expr)
The complete syntax is as follows:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
Parameter definition
| Parameters | Description |
|---|---|
| expr | Required parameters. It specifies one or more columns or expressions to join. |
| DISTINCT | Optional parameters. It is used to eliminate duplicate values. |
| ORDER BY | Optional parameters. It is used to sort the content to be connected. By default, it sorts the values in ascending order. If you want to sort the values in descending order, you need to specify the DESC option explicitly. |
| SEPARATOR | Optional parameters. Connector. The default is ,. |
Return value
The return value is a non-binary or binary string, depending on whether the parameter expr is a non-binary or binary string.
If the result set does not have any rows, this function returns NULL.
Example
create table t1(a int,b text,c text);
insert into t1 values(1,"a","bc"),(2,"ab","c"),(3,"aa","bb"),(3,"aa","bb");
mysql> select group_concat(distinct a,b,c separator '|') from t1;
+------------------------------------------+
| group_concat(distinct a, b, c, |) |
+------------------------------------------+
| 1abc|2abc|3aabb|
+------------------------------------------+
1 row in set (0.01 sec)
mysql> select group_concat(distinct b,c separator '|') from t1 group by a;
+------------------------------------+
| group_concat(distinct b, c, |) |
+------------------------------------+
| abc |
| abc |
| aabb |
+------------------------------------+
3 rows in set (0.01 sec)
mysql> select group_concat(distinct b,c separator '|') from t1;
+------------------------------------+
| group_concat(distinct b, c, |) |
+------------------------------------+
| abc|abc|aabb|
+------------------------------------+
1 row in set (0.01 sec)