Mysql-拼接字串

來源:互聯網
上載者:User

標籤:修改   log   配置   檔案   ssi   concat_ws   keyword   body   gpo   

1. CONCAT(string1,string2,…) 

SELECT CONCAT(‘L7的球衣號為‘, L7Number) FROM table

如果其中一個參數為NULL,則拼接後的結果為NULL

 

2.CONCAT_WS(separator,str1,str2,...)

separator代表分隔字元,如果分隔字元為NULL,則拼接後的結果為NULL

例1: CONCAT_WS會自動忽略null值

mysql> select concat_ws(‘#‘,‘dbdh=‘,‘NorthEastTrcoon‘,null) AS dbdh_name_three;
+-----------------------+
| dbdh_name_three       |
+-----------------------+
| dbdh=#NorthEastTrcoon |
+-----------------------+
1 row in set (0.00 sec)

例2: CONCAT_WS的分隔字元如果為null,則結果為null

  mysql> select concat_ws(null,‘dbdh=‘,‘NorthEastTrcoon‘,null) AS dbdh_name_fourth
;
+------------------+
| dbdh_name_fourth |
+------------------+
| NULL             |
+------------------+
1 row in set (0.00 sec)

 

3. group_concat

將分組後,在同一組的資料拼接出來,預設以逗號分割。group_concat()函數需要與group by語句在一起使用,才能得到需要的效果。

使用情境:假如需要查詢的結果是這樣:左邊顯示組名,右邊想顯示該組別下的所有成員資訊。

完整的文法如下:
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)

 

注意:group_concat是有預設長度的,這個地方有坑。之前項目中希望用group_concat出來的結果,作為FIND_IN_SET的查詢項。結果一直不對。最後發現是group_concat出的結果過長了。所以一定得注意這個地方。設定長度的方法

 

修改MySQL的設定檔:

 

#需要設定的長度group_concat_max_len = 5120

 

 也可以使用sql語句設定:

 

SET GLOBAL group_concat_max_len=5120;SET SESSION group_concat_max_len=5120;

 

 

4. repeat()函數

用來複製字串,如下‘ab‘表示要複製的字串,2表示複製的份數

mysql> select repeat(‘ab‘,2);

+----------------+
| repeat(‘ab‘,2) |
+----------------+
| abab           |
+----------------+

1 row in set (0.00 sec)

又如
mysql> select repeat(‘a‘,2);

+---------------+
| repeat(‘a‘,2) |
+---------------+
| aa            |
+---------------+
1 row in set (0.00 sec)

 

Mysql-拼接字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.