Mysql 函数concat、concat_ws和group_concat

Mysql 函数concat、concat_ws和group_concat

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

  • concat

  • concat_ws

  • group_concat

image

concat

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

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

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

image
-- 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)

注意两种情况的不同:

image

concat_ws

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

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

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

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

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

-- 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:连接。合起来就是分组连接,具体语法为:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="sql" cid="n84" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> GROUP_CONCAT(DISTINCT expression ORDER BY expression SEPARATOR sep);</pre>

<pre mdtype="fences" cid="n118" lang="sql" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-color: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 0px; width: inherit; background-position: initial initial; background-repeat: initial initial;"> set session group_concat_max_len=18783847439738273; -- 防止超出范围数据被截掉</pre>

image

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

  • DISTINCT子句用于在连接分组之前消除组中的重复值

  • ORDER BY 连接之前按升序或者降序排列。默认是升序

  • SEPARATOR指定在组中的值之间插入的文字值。如果不指定分隔符,则GROUP_CONCAT函数使用逗号()作为默认分隔符

  • 函数会自动忽略NULL值,如果所有的参数都是NULL,则结果返回NULL

  • GROUP_CONCAT函数返回二进制或非二进制字符串,取决于参数。 默认情况下,返回字符串的最大长度为1024。通过在SESSIONGLOBAL级别设置group_concat_max_len系统变量来扩展最大长度。

-- 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 后面的字段是相同的

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容