MySQL – 字串連接、合併欄位

from 
http://www.hkcode.com/programming/359 
 

有時我們需要將不同欄位的資料連接,或者在現在的資料後面加入內容,當然可以先從 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'

———————————————————–
另外也可以這樣合併欄位:
例如

select     merchantnumber as id,
                paytime as datetime,
                concat('文字',amount)    as inorde,
                ' + '     as     lcash,
                idn     as    idn
 from cash_bill

發表迴響