前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql 函数concat、concat_ws和group_concat

Mysql 函数concat、concat_ws和group_concat

原创
作者头像
皮大大
修改2021-01-22 11:02:01
2.5K0
修改2021-01-22 11:02:01
举报

Mysql 函数concat、concat_ws和group_concat

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

  • concat
  • concat_ws
  • group_concat

concat

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

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

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

-- 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本身就是字符串,则结果不相同:

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

注意两种情况的不同:

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

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系统变量来扩展最大长度。
set session group_concat_max_len=18783847439738273;  -- 防止超出范围数据被截掉

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

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Mysql 函数concat、concat_ws和group_concat
  • concat
  • concat_ws
  • group_concat
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档