本文介绍的是MySQL
中3个函数的使用,主要是针对字符串的连接合并处理:
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)
上面的NULL
是MySQL
中NULL
,如果NULL
本身就是字符串,则结果不相同:
mysql> select concat("01","赵雷","NULL","男");
+------------------------------------+
| concat("01","赵雷","NULL","男") |
+------------------------------------+
| 01赵雷NULL男 |
+------------------------------------+
1 row in set (0.01 sec)
注意两种情况的不同:
concat_ws()
函数相比较于concat()
多了一个指定的连接符号,语法为:
concat_ws(separator, str1, str2, str3)
连接符要放在待连接的字符之间;分隔符也可以是一个字符串,也可以是其他的参数,需要注意的是:
下面通过几个例子来说明使用方法:
-- 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(DISTINCT expression ORDER BY expression SEPARATOR sep);
DISTINCT
子句用于在连接分组之前消除组中的重复值ORDER BY
连接之前按升序或者降序排列。默认是升序SEPARATOR
指定在组中的值之间插入的文字值。如果不指定分隔符,则GROUP_CONCAT
函数使用逗号(,
)作为默认分隔符GROUP_CONCAT
函数返回二进制或非二进制字符串,取决于参数。 默认情况下,返回字符串的最大长度为1024
。通过在SESSION
或GLOBAL
级别设置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 后面的字段是相同的