Fork me on GitHub

MySQL函数-concat、concat_ws、group_concat

Mysql 函数concat、concat_ws和group_concat

本文介绍的是MySQL中3个函数的使用,主要是针对字符串的连接合并处理:

  • concat
  • concat_ws
  • group_concat

concat

concat()函数是将多个字符串组合在一起,形成一个大的字符串;如果连接的字符串中存在一个为NULL,则输出的结果为NULL,语法格式为:

1
concat(str1,str2,....strn)

3个例子🌰说明具体使用,以下面这个表中的第一条记录为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- 1、字符之间不加连接符
mysql> select concat("01","赵雷","男");
+-----------------------------+
| concat("01","赵雷","男") |
+-----------------------------+
| 01赵雷男 |
+-----------------------------+
1 row in set (0.00 sec)

-- 2、字符之间添加连接符
mysql> select concat("01-","赵雷-","男");
+-------------------------------+
| concat("01-","赵雷-","男") |
+-------------------------------+
| 01-赵雷-男 |
+-------------------------------+
1 row in set (0.00 sec)

-- 3、忽略空字符串
mysql> mysql> select concat("01","赵雷","","男");
+--------------------------------+
| concat("01","赵雷","","男") |
+--------------------------------+
| 01赵雷男 |
+--------------------------------+
1 row in set (0.00 sec)


-- 4、存在NULL的情况
mysql> select concat("01","赵雷",NULL,"男"); -- 结果直接显示为NULL
+----------------------------------+
| concat("01","赵雷",NULL,"男") |
+----------------------------------+
| NULL |
+----------------------------------+
1 row in set (0.01 sec)

上面的NULLMySQLNULL,如果NULL本身就是字符串,则结果不相同:

1
2
3
4
5
6
7
mysql> select concat("01","赵雷","NULL","男");
+------------------------------------+
| concat("01","赵雷","NULL","男") |
+------------------------------------+
| 01赵雷NULL男 |
+------------------------------------+
1 row in set (0.01 sec)

注意两种情况的不同:

concat_ws

concat_ws()函数相比较于concat()多了一个指定的连接符号,语法为:

1
concat_ws(separator, str1, str2, str3)
  • 第一个参数是连接的符号
  • 后面的参数是待连接的字符

连接符要放在待连接的字符之间;分隔符也可以是一个字符串,也可以是其他的参数,需要注意的是:

  1. 如果分隔符是NULL,结果为NULL
  2. 函数后忽略任何分割符参数后的NULL值(分隔符之后的NULL值):连接的时候跳过NULL值
  3. concat_ws不会忽略空字符串;concat会忽略空字符串

下面通过几个例子来说明使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- 1、指定不同的连接符号:分别指定逗号和加号

mysql> select concat_ws(",","01","赵雷","男");
+------------------------------------+
| concat_ws(",","01","赵雷","男") |
+------------------------------------+
| 01,赵雷,男 |
+------------------------------------+
1 row in set (0.00 sec)

mysql> select concat_ws("+","01","赵雷","男");
+------------------------------------+
| concat_ws("+","01","赵雷","男") |
+------------------------------------+
| 01+赵雷+男 |
+------------------------------------+
1 row in set (0.00 sec)

-- 2、不忽略空字符串
mysql> select concat_ws("+","01","赵雷","","男");
+---------------------------------------+
| concat_ws("+","01","赵雷","","男") |
+---------------------------------------+
| 01+赵雷++男 |
+---------------------------------------+
1 row in set (0.00 sec)

-- 3、忽略NULL;不管几个NULL都会忽略
mysql> select concat_ws("+","01","赵雷",NULL,"男");
+-----------------------------------------+
| concat_ws("+","01","赵雷",NULL,"男") |
+-----------------------------------------+
| 01+赵雷+男 |
+-----------------------------------------+
1 row in set (0.00 sec)

-- 忽略两个NULL
mysql> select concat_ws("+","01",NULL,"赵雷",NULL,"男");
+----------------------------------------------+
| concat_ws("+","01",NULL,"赵雷",NULL,"男") |
+----------------------------------------------+
| 01+赵雷+男 |
+----------------------------------------------+
1 row in set (0.00 sec)

group_concat

group:分组的意思;concat:连接。合起来就是分组连接,具体语法为:

1
GROUP_CONCAT(DISTINCT expression ORDER BY expression SEPARATOR sep);
  • DISTINCT子句用于在连接分组之前消除组中的重复值
  • ORDER BY 连接之前按升序或者降序排列。默认是升序
  • SEPARATOR指定在组中的值之间插入的文字值。如果不指定分隔符,则GROUP_CONCAT函数使用逗号()作为默认分隔符
  • 函数会自动忽略NULL值,如果所有的参数都是NULL,则结果返回NULL
  • GROUP_CONCAT函数返回二进制或非二进制字符串,取决于参数。 默认情况下,返回字符串的最大长度为1024。通过在SESSIONGLOBAL级别设置group_concat_max_len系统变量来扩展最大长度。
1
set session group_concat_max_len=18783847439738273;  -- 防止超出范围数据被截掉

下面通过这张成绩表Score来讲解:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- 1、将每个学生的成绩单独列出来
mysql> select s_id, group_concat(s_score) from Score group by s_id;
+------+-----------------------+
| s_id | group_concat(s_score) |
+------+-----------------------+
| 01 | 80,90,96 |
| 02 | 70,60,80 |
| 03 | 80,81,85 |
| 04 | 50,40,30 |
| 05 | 76,87 |
| 06 | 43,56 |
| 07 | 89,94 |
+------+-----------------------+
7 rows in set (0.01 sec)

-- 2、指定连接符+
mysql> select s_id, group_concat(s_score separator "+") from Score group by s_id;
+------+-------------------------------------+
| s_id | group_concat(s_score separator "+") |
+------+-------------------------------------+
| 01 | 80+90+96 |
| 02 | 70+60+80 |
| 03 | 80+81+85 |
| 04 | 50+40+30 |
| 05 | 76+87 |
| 06 | 43+56 |
| 07 | 89+94 |
+------+-------------------------------------+
7 rows in set (0.00 sec)

-- 3、指定排序的字段
-- 分数s_score已经完成了排序(指定了降序);上面的结果不指定则默认是降序
mysql> select s_id, group_concat(distinct s_score order by s_score desc separator "+") from Score group by s_id;
+------+--------------------------------------------------------------------+
| s_id | group_concat(distinct s_score order by s_score desc separator "+") |
+------+--------------------------------------------------------------------+
| 01 | 96+90+80 |
| 02 | 80+70+60 |
| 03 | 85+81+80 |
| 04 | 50+40+30 |
| 05 | 87+76 |
| 06 | 56+43 |
| 07 | 94+89 |
+------+--------------------------------------------------------------------+
7 rows in set (0.00 sec)


-- 4、去重操作
-- distinct s_score表示对分数去重,取出每个学生的不同分数(表中每个学生的分数都不相同,结果同上)
mysql> select s_id, group_concat(distinct s_score order by s_score desc separator "+") from Score group by s_id;
+------+--------------------------------------------------------------------+
| s_id | group_concat(distinct s_score order by s_score desc separator "+") |
+------+--------------------------------------------------------------------+
| 01 | 96+90+80 |
| 02 | 80+70+60 |
| 03 | 85+81+80 |
| 04 | 50+40+30 |
| 05 | 87+76 |
| 06 | 56+43 |
| 07 | 94+89 |
+------+--------------------------------------------------------------------+
7 rows in set (0.00 sec)

distinct 和order by 后面的字段是相同的

本文标题:MySQL函数-concat、concat_ws、group_concat

发布时间:2021年01月21日 - 22:01

原始链接:http://www.renpeter.cn/2021/01/21/MySQL%E5%87%BD%E6%95%B0-concat%E3%80%81concat-ws%E3%80%81group-concat.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea