Oracle与Mysql的字符连接函数concat与||

时间:2022-09-07 15:48:59

1Oracle版本:

1.1、执行语句:SELECT 'k' as a1,'a' as a2,'y' as a3FROM sr_main

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

1 k a y

2 k a y

3 k a y

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

1.2执行语句:SELECT concat( 'k','a') as pjFROM sr_main

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

1 ka

2 ka

3 ka

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

1.3执行语句:SELECT concat( 'k','a','y') as pjFROM 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

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

此方法可以实现两者通用