sql拼接同一字段_sql多个字段拼接
1. MySQL
在Java等編程語言匯總字符串的拼接可以通過加號“+”來實(shí)現(xiàn),比如:"1"+"3"、"a"+"b"。在MySQL中也可以使用加號“+”來連接兩個(gè)字符串比如下面的SQL:
SELECT '12'+'33', FAge+'1' FROM T_Employee
+-----------+--------+
| '12'+'33' | FAge+1 |
+-----------+--------+
|??????? 45 |???? 26 |
|??????? 45 |???? 29 |
|??????? 45 |?? NULL |
|??????? 45 |???? 24 |
|??????? 45 |???? 26 |
|??????? 45 |???? 29 |
|??????? 45 |???? 28 |
|??????? 45 |???? 26 |
|??????? 45 |???? 36 |
+-----------+--------+
仔細(xì)觀察第一列,驚訝嗎?這個(gè)列的現(xiàn)實(shí)結(jié)果并不是我們希望的“1233”,而是把“12”和“33”兩個(gè)字符串當(dāng)成數(shù)字來求兩個(gè)數(shù)的和了;將一個(gè)數(shù)字與一個(gè)字符串用加號“+”鏈接也是同樣的效果,比如這里的第二列。
在MySQL中進(jìn)行字符串的拼接要使用CONCAT()函數(shù),CONCAT()函數(shù)支持一個(gè)或者多個(gè)參數(shù),參數(shù)類型可以為字符串類型也可以是非字符串類
型。對于非字符串類型的參數(shù)MySQL將嘗試將其轉(zhuǎn)化為字符串類型,CONCAT()函數(shù)會(huì)將所有參數(shù)按照參數(shù)的順序拼接成一個(gè)字符串作為返回值。比如下
面的SQL語句用于將用戶的多個(gè)字段信息以一個(gè)字段的形式查詢出來:
SELECT CONCAT('Staff Number :', 'Happiness Index', FNumber, FSalary/(FAge-21)) from t_employee
+----------------------------------------------------------+
| concat?????????????????????????????????????????????????? |
+----------------------------------------------------------+
| Staff number: Happiness index of DEV001 is 2075.000000?? |
| Staff number: Happiness index of DEV002 is 328.571429??? |
| NULL??????????????????????????????????????????????????? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? |
| Staff number: Happiness index of HR001 is 2150.000000??? |
| Staff number: Happiness index of HR002 is 825.000000???? |
| Staff number: Happiness index of IT001 is 785.714286???? |
| Staff number: Happiness index of IT002 is 466.666667???? |
| Staff number: Happiness index of SALES001 is 1325.000000 |
| Staff number: Happiness index of SALES002 is 592.857143? |
+----------------------------------------------------------+
MYSQL中還提供了另外一個(gè)進(jìn)行字符串拼接的函數(shù)CONCAT_WS,CONCAT_WS可以在待拼接的字符串之間加入指定的分割符。它的第一個(gè)參數(shù)值為采用的分隔符,而剩下的參數(shù)則為待拼接的字符串值,比如執(zhí)行下面的SQL:
SELECT CONCAT_WS(', ', FNumber,FAge, FDepartment, FSalary) FROM T_Employee
+------------------------------------------------------+
| concat_ws(', ', FNumber, FAge, FDepartment, FSalary) |
+------------------------------------------------------+
| DEV001, 25, Development, 8300.00???????????????????? |
| DEV002, 28, Development, 2300.00???????????????????? |
| DEV003, Development, 3333.00???????????????????????? |
| HR001, 23, HumanResource, 4300.00??????????????????? |
| HR002, 25, HumanResource, 3300.00??????????????????? |
| IT001, 28, InfoTech, 5500.00???????????????????????? |
| IT002, 27, InfoTech, 2800.00???????????????????????? |
| SALES001, 25, Sales, 5300.00???????????????????????? |
| SALES002, 35, Sales, 8300.00???????????????????????? |
+------------------------------------------------------+
2. Oracle
Oracle中使用雙豎杠"||"進(jìn)行字符串拼接。比如下面的SQL語句:
SELECT '工號為' || FNumber || '的員工姓名為'||FName FROM T_employee where fname is not null
除了"||", Oracle還支持使用CONCAT()函數(shù)進(jìn)行字符串拼接,比如執(zhí)行下面的SQL語句:
SELECT CONCAT('工號: ' , FNumber) FROM T_Employee
與MySQL的CONCAT()函數(shù)不同,Oracle的CONCAT()函數(shù)只支持兩個(gè)參數(shù),不支持兩個(gè)以上的字符串的拼接
如果要進(jìn)行多個(gè)字符串的拼接的話,可以將多個(gè)CONCAT()函數(shù)嵌套使用,上面的SQL可以改寫如下:
SELECT CONCAT(CONCAT(CONCAT('工號為', FNumber), '的員工姓名為'), FName) FROM T_Employee WHERE FName IS NOT NULL
總結(jié)
以上是生活随笔為你收集整理的sql拼接同一字段_sql多个字段拼接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说说最近的读书计划
- 下一篇: 2020年,拥抱不平凡