第一种:
MySQL自带语法Concat(string1,string2,string3...),此处是直接把string1和string2等等的字符串拼接起来(无缝拼接哦)
说明:此方法在拼接的时候如果有一个值为NULL,则返回NULL
select concat("aaa","bbbb","ccccc") as str
select concat("aaa","bbbb",null) as str
第二种:
第二种也是mysql自带语法CONCAT_WS(separator,string1,string2,...),但是可以多个字符串用指定的字符串进行拼接(带缝拼接哦)
说明:string1,string2代表的是字符串,而separator代表的是连接其他参数的分隔符,可以是符号,也可以是字符串。如果分隔符为NULL,则结果为NULL。此方法参数可以为NULL。
select 1 as test1, CONCAT_WS("aaa",null,"cccc") as str union all
select 2 , CONCAT_WS("aaa","bbbb",null) union all
select 3 , CONCAT_WS("aaa","null","cccc") union all
select 4 , CONCAT_WS("aaa","bbbb","null") union all
select 5 , CONCAT_WS("aaa","bbbb","cccc") union all
select 6 , CONCAT_WS(null,"bbbb","cccc") union all
select 7 , CONCAT_WS("null","bbbb","cccc")
更多推荐
MySQL字符串拼接的两种方式
发布评论