字符串连接函数CONCAT和CONCAT_WS

在mysql的SQL语句中,进行字符串连接不能使用+或||,可以使用concat函数来完成.

如要将users表中的name字段的数内容修改成"greenskys"+userid.其SQL语句为:

update users set name=concat(´greenskys´,userid);

这里concat只连接了两个字符串,concat函数可以一次连接多个字符串.

用法为:concat(string1,string2,string3.......).

另一个衍生函数是concat_ws(separator,string1,string2,...)

它与concat的区别在于,连接后,在连接处有一个分隔字符separator.如:

update users set homedir=concat_ws(´/´,´/data/disk1´,´part1´,´user1´) where userid=´user1´;

其结果是user1的homedir=´/data/disk1/part1/user1´

特殊情况:

concat(´greenskys´,NULL,´kkkkk´)=NULL

concat(12.3)=´12.3´

concat_ws(´,´,´greenskys´,NULL,´hahahaha´)=´greenskys,hahahaha´

posted on 2009-04-17 17:51  alon  阅读(878)  评论(0编辑  收藏  举报

导航