1. 程式人生 > >Oracle與Mysql的字元連線函式concat與||

Oracle與Mysql的字元連線函式concat與||

1Oracle版本:

1.1、執行語句:SELECT 'k' as a1,'a' as a2 ,'y' as a3 FROM sr_main

--------------------------------------------------------------------------------------------

1 k a y

2 k a y

3 k a y

--------------------------------------------------------------------------------------------

1.2執行語句:SELECT concat

( 'k','a') as pj FROM sr_main

--------------------------------------------------------------------------------------------

1 ka

2 ka

3 ka

--------------------------------------------------------------------------------------------

1.3執行語句:SELECT concat( 'k','a','y') as pj FROM sr_main

--------------------------------------------------------------------------------------------

Errorinvalid number of arguments

--------------------------------------------------------------------------------------------

ReasonConcat只能連線兩個字串

1.4執行語句:SELECT  'k'||'a'||'y' as pj  FROM sr_main

--------------------------------------------------------------------------------------------

1 kay

2 kay

3 kay

--------------------------------------------------------------------------------------------

2Mysql版本:

2.1執行語句:SELECT concat( 'k','a') as pj FROM sr_main

--------------------------------------------------------------------------------------------

ka

ka

Ka

--------------------------------------------------------------------------------------------

2.2執行語句:SELECT concat( 'k','a','y') as pj FROM sr_main

--------------------------------------------------------------------------------------------

kay

kay

Kay

--------------------------------------------------------------------------------------------

2.3執行語句:SELECT  'k'||'a'||'y' as pj  FROM sr_main

--------------------------------------------------------------------------------------------

0

0

0

--------------------------------------------------------------------------------------------

分析兩者差異:

Oracle中可以用||拼接多個字串,但用concat只能拼兩個,否則報錯

Mysql中可以用concat拼接多個,但用||無法拼接字串,會顯示零

綜合兩者:SELECT concat(concat( 'k','a'),'y') as pj FROM sr_main

--------------------------------------------------------------------------------------------

kay

kay

Kay

--------------------------------------------------------------------------------------------

此方法可以實現兩者通用