1. 程式人生 > >SQL 字串拼接

SQL 字串拼接

Sql Server

一、拼接多個欄位的值
select Convert(nvarchar(50),id)+’-’+name+’-’+sex as montage from test
二、一個欄位多條記錄的拼接
select stuff((select ‘-’+name from test for xml path (’’)),1,1,’’) as montage
①stuff:
1、作用
stuff(param1, startIndex, length, param2)
將param1中自startIndex(SQL中都是從1開始,而非0)起,刪除length個字元,然後用param2替換刪掉的字元。
2、引數
param1
一個字元資料表示式。param1可以是常量、變數,也可以是字元列或二進位制資料列。
startIndex
一個整數值,指定刪除和插入的開始位置。如果 startIndex或 length 為負,則返回空字串。如果startIndex比param1長,則返回空字串。startIndex可以是 bigint 型別。
length
一個整數,指定要刪除的字元數。如果 length 比param1長,則最多刪除到param1 中的最後一個字元。length 可以是 bigint 型別。
3、返回型別
如果param1是受支援的字元資料型別,則返回字元資料。如果param1是一個受支援的 binary 資料型別,則返回二進位制資料。
4、備註
如果結果值大於返回型別支援的最大值,則產生錯誤。
②for xml path:
for xml path有的人可能知道有的人可能不知道,其實它就是將查詢結果集以XML形式展現,有了它我們可以簡化我們的查詢語句實現一些以前可能需要藉助函式活儲存過程來完成的工作。

MySql

在Mysql 資料庫中存在兩種字串連線操作.具體操作如下
一. 語法

  1. CONCAT(string1,string2,…) 說明 : string1,string2代表字串,concat函式在連線字串的時候,只要其中一個是NULL,那麼將返回NULL

  2. CONCAT_WS(separator,str1,str2,…)
    說明 : string1,string2代表字串,concat_ws 代表 concat with separator,第一個引數是其它引數的分隔符。分隔符的位置放在要連線的兩個字串之間。分隔符可以是一個字串,也可以是其它引數。如果分隔符為 NULL,則結果為 NULL。函式會忽略任何分隔符引數後的 NULL 值。
    舉例1:
    mysql> select concat_ws(’#’,‘dbdh=’,‘NorthEastTrcoon’,null) AS dbdh_name_three;
    ±----------------------+
    | dbdh_name_three |
    ±----------------------+
    | dbdh=#NorthEastTrcoon |
    ±----------------------+
    1 row in set (0.00 sec)
    例2:
    mysql> select concat_ws(null,‘dbdh=’,‘NorthEastTrcoon’,null) AS dbdh_name_fourth
    ;
    ±-----------------+
    | dbdh_name_fourth |
    ±-----------------+
    | NULL |
    ±-----------------+
    1 row in set (0.00 sec)
    例3
    mysql> select concat_ws(’*’,‘dbdh=’,‘NorthEastTrcoon’,null) AS dbdh_name_fifth;
    ±----------------------+
    | dbdh_name_fifth |
    ±----------------------+
    | dbdh=*NorthEastTrcoon |
    ±----------------------+
    1 row in set (0.00 sec)

  3. MySQL中group_concat函式
    完整的語法如下:
    group_concat([DISTINCT] 要連線的欄位 [Order BY ASC/DESC 排序欄位] [Separator ‘分隔符’])
    基本查詢
    mysql> select * from stu1;
    ±-----±-----+
    | id| name |
    ±-----±-----+
    |1 | 10|
    |1 | 20|
    |1 | 20|
    |2 | 20|
    |3 | 200 |
    |3 | 500 |
    ±-----±-----+
    6 rows in set (0.00 sec)
    以id分組,把name欄位的值列印在一行,逗號分隔(預設)

mysql> select id,group_concat(name) from aa group by id;
±-----±-------------------+
| id| group_concat(name) |
±-----±-------------------+
|1 | 10,20,20|
|2 | 20 |
|3 | 200,500|
±-----±-------------------+
3 rows in set (0.00 sec)
以id分組,把name欄位的值列印在一行,分號分隔
mysql> select id,group_concat(name separator ‘;’) from aa group by id;
±-----±---------------------------------+
| id| group_concat(name separator ‘;’) |
±-----±---------------------------------+
|1 | 10;20;20 |
|2 | 20|
|3 | 200;500 |
±-----±---------------------------------+
3 rows in set (0.00 sec)
以id分組,把去冗餘的name欄位的值列印在一行,
逗號分隔
mysql> select id,group_concat(distinct name) from aa group by id;
±-----±----------------------------+
| id| group_concat(distinct name) |
±-----±----------------------------+
|1 | 10,20|
|2 | 20 |
|3 | 200,500 |
±-----±----------------------------+
3 rows in set (0.00 sec)

以id分組,把name欄位的值列印在一行,逗號分隔,以name排倒序

mysql> select id,group_concat(name order by name desc) from aa group by id;
±-----±--------------------------------------+
| id| group_concat(name order by name desc) |
±-----±--------------------------------------+
|1 | 20,20,10 |
|2 | 20|
|3 | 500,200|
±-----±--------------------------------------+
3 rows in set (0.00 sec)
還有一個簡單的連線方式為: ||
mysql> select * from student;
±—±-----±------±---------±-----------+
| id | age | score | name | birth |
±—±-----±------±---------±-----------+
| 1 | 23 | 78 | 李四 | 2017-10-10 |
| 2 | 24 | 78 | zhangsan | 2017-10-10 |
| 3 | 25 | 99 | 王五 | 2016-05-17 |
±—±-----±------±---------±-----------+
3 rows in set (0.00 sec)

mysql> select id+999,name,name+99,name+‘999’ from student;
±-------±---------±--------±-----------+
| id+999 | name | name+99 | name+‘999’ |
±-------±---------±--------±-----------+
| 1000 | 李四 | 99 | 999 |
| 1001 | zhangsan | 99 | 999 |
| 1002 | 王五 | 99 | 999 |
±-------±---------±--------±-----------+
3 rows in set, 6 warnings (0.00 sec)