concat函数用于将两个字符串连接为一个字符串,试一下下面这个例子:
select concat("first ", "second"); ---------------------------- | concat("first ", "second") | ---------------------------- | first second | ----------------------------
要对 concat函数有更为深入的了解,请考虑 employee_tbl表,表中记录如下所示:
(资料图)
select * from employee_tbl; ------ ------ ------------ -------------------- | id | name | work_date | daily_typing_pages | ------ ------ ------------ -------------------- | 1 | john | 2022-08-24 | 250 | | 2 | ramr | 2022-08-27 | 220 | | 3 | jack | 2022-08-06 | 170 | | 3 | jack | 2022-08-06 | 100 | | 4 | jill | 2022-08-06 | 220 | | 5 | zara | 2022-08-06 | 300 | | 5 | zara | 2022-08-06 | 350 | ------ ------ ------------ --------------------
现在,假设你想要将上表中所有的姓名(name)、id和工作日(work_date)连接在一起,那么可以通过如下的命令来达到目的:
select concat(id, name, work_date) from employee_tbl; ----------------------------- | concat(id, name, work_date) | ----------------------------- | 1john2022-08-24 | | 2ramr2022-08-27 | | 3jack2022-08-06 | | 3jack2022-08-06 | | 4jill2022-08-06 | | 5zara2022-08-06 | | 5zara2022-08-06 | -----------------------------
还可以在字符中间添加符号
select concat(id,",",name,",",work_date) from employee_tbl; ----------------------------- | concat(id, name, work_date) | ----------------------------- | 1,john,2022-08-24 | | 2,ramr,2022-08-27 | | 3,jack,2022-08-06 | | 3,jack,2022-08-06 | | 4,jill,2022-08-06 | | 5,zara,2022-08-06 | | 5,zara,2022-08-06 | ----------------------------- select concat(id,",",name,",",work_date) from employee_tbl; ----------------------------- | concat(id, name, work_date) | ----------------------------- | 1,john,2022-08-24 | | 2,ramr,2022-08-27 | | 3,jack,2022-08-06 | | 3,jack,2022-08-06 | | 4,jill,2022-08-06 | | 5,zara,2022-08-06 | | 5,zara,2022-08-06 | -----------------------------
concat_ws() 代表 concat with separator ,是concat()的 特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是 一个字符串,也可以是其它参数。
select concat(",",id,name,work_date) from employee_tbl; ----------------------------- | concat(id, name, work_date) | ----------------------------- | 1,john,2022-08-24 | | 2,ramr,2022-08-27 | | 3,jack,2022-08-06 | | 3,jack,2022-08-06 | | 4,jill,2022-08-06 | | 5,zara,2022-08-06 | | 5,zara,2022-08-06 | -----------------------------
我们可以使用group_concat()函数将一整列打印到一行中,并且可以指定分隔的符号,默认为
,
select group_concat(id) as ids from employee_tbl ------------------ | group_concat(id) | ------------------ | 1,2,3,3,4,5,5 | ------------------
将分隔符改为;
select group_concat(id separator ";") from employee_tbl; ------------------ | group_concat(id) | ------------------ | 1;2;3;3;4;5;5 | ------------------
功能介绍:将多个字符串进行拼接。
语法:concat(字段,字段,字段,…)
concat实践
select concat(id,",",age,",",name) as line_info from user
如果连接串中存在null,则返回结果为null(我没遇到过):
select concat(id,",",age,",",null) as line_info from user
null凯发app的解决方案:
加一个 ifnull()函数。
select concat(id,",",age,",",ifnull(name,"")) as line_info from user
concat_ws
功能介绍:字符串拼接无需手动添加分隔符(自己理解)。
语法:concat_ws(分隔符,字段,字段,…)
concat_ws实践
select concat_ws(",",id,age,name) as line_info from user
select concat_ws(",",concat(name),concat("年龄",age,"岁")) as line_info from user
到此这篇关于mysql函数concat、concat_ws、group_concat用法详解的文章就介绍到这了,更多相关mysql函数concat concat_ws、group_concat内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
免责声明:本文不构成任何商业建议,投资有风险,选择需谨慎!本站发布的图文一切为分享交流,传播正能量,此文不保证数据的准确性,内容仅供参考
关键词: