MySQL 字串连接


有时我们需要将不同字段的资料连接,或者在现在的资料后面加入内容,当然可以先从 MySQL 汇出资料,再把连接好的字串更新到 MySQL,但这未免太麻烦,另一个较方便的方法是借助 CONCAT() 实现,CONCAT() 语法为:

CONCAT(string_1, string_2, string_3, …)

MySQL 的 CONCAT() 支援多个字串连接,但在 Oracle 里面只可以有两个参数,要实现多个字串连接可以用 ‘||’ 来实现。以下是 CONCAT() 的使用实例:

update table_name set email=CONCAT(email, ‘:me@email.com’) where id=’1′;

以上 SQL 语法会更新 table_name 资料表的 email 字段,在原本的资料后面加入 ‘:me@email.com’

Tags:

Leave a Reply