Mysql-拼接字符串

1. CONCAT(string1,string2,…) mysql

SELECT CONCAT('L7的球衣號爲', L7Number) FROM tablesql

若是其中一個參數爲NULL,則拼接後的結果爲NULL函數

 

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

separator表明分隔符,若是分隔符爲NULL,則拼接後的結果爲NULLcode

例1: CONCAT_WS會自動忽略null值blog

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,則結果爲nullthree

  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_concatstring

將分組後,在同一組的數據拼接出來,默認以逗號分割。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)

相關文章
相關標籤/搜索