- 返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL
- 参数可以是字段名或具体值
注意:
- 如果所有参数均为非二进制字符串,则结果为非二进制字符串
- 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串
- 一个数字参数被转化为与之相等的二进制字符串格式
- 若要避免这种情况,可使用显式类型 cast, 例如:
SELECT CONCAT(CAST(int_col AS CHAR), char_col)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| mysql> select concat('10');
+--------------+
| concat('10') |
+--------------+
| 10 |
+--------------+
1 row in set (0.00 sec)
mysql> select concat('11','22','33');
+------------------------+
| concat('11','22','33') |
+------------------------+
| 112233 |
+------------------------+
1 row in set (0.00 sec)
|
1
2
3
4
5
6
7
| mysql> select concat('11','22',null);
+------------------------+
| concat('11','22',null) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
|
CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。
注意:
- 参数可以是字段名或具体值
- 如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。(只保留非空值)
1
2
3
4
5
6
7
| mysql> select concat_ws('','','','33');
+--------------------------+
| concat_ws('','','','33') |
+--------------------------+
| 33 |
+--------------------------+
1 row in set (0.04 sec)
|
1
2
3
4
5
6
7
| mysql> select concat_ws(',','11','22','33');
+-------------------------------+
| concat_ws(',','11','22','33') |
+-------------------------------+
| 11,22,33 |
+-------------------------------+
1 row in set (0.00 sec)
|
1
2
3
4
5
6
7
| mysql> select concat_ws(',','11','22',NULL);
+-------------------------------+
| concat_ws(',','11','22',NULL) |
+-------------------------------+
| 11,22 |
+-------------------------------+
1 row in set (0.00 sec)
|
1
| group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator ‘分隔符’])
|
1
2
3
4
5
6
7
8
9
10
11
12
| mysql> select * from aa;
+------+------+
| id| name |
+------+------+
|1 | 10|
|1 | 20|
|1 | 20|
|2 | 20|
|3 | 200 |
|3 | 500 |
+------+------+
6 rows in set (0.00 sec)
|
1
2
3
4
5
6
7
8
9
| mysql> select id,group_concat(name) from aa group by id;
+------+--------------------+
| id| group_concat(name) |
+------+--------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
+------+--------------------+
3 rows in set (0.00 sec)
|
1
2
3
4
5
6
7
8
9
| mysql> select id,group_concat(distinct name) from aa group by id;
+------+-----------------------------+
| id| group_concat(distinct name) |
+------+-----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
+------+-----------------------------+
3 rows in set (0.00 sec)
|
1
2
3
4
5
6
7
8
9
| mysql> select id,group_concat(name order by name desc) from aa group by id;
+------+---------------------------------------+
| id| group_concat(name order by name desc) |
+------+---------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
+------+---------------------------------------+
3 rows in set (0.00 sec)
|
https://stackoverflow.com/questions/11889391/difference-between-group-concat-and-concat-ws
http://www.w3resource.com/mysql/string-functions/mysql-concat_ws-function.php
http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php