1. 程式人生 > 實用技巧 >MYSQL中CONCAT、CONCAT_WS、GROUP_CONCAT詳解

MYSQL中CONCAT、CONCAT_WS、GROUP_CONCAT詳解

concat()函式

1. 功能:

返回結果為連線引數產生的字串。如有任何一個引數為NULL ,則返回值為 NULL。

2. 語法

concat(str1, str2,...)

3. 例子

案例一:

mysql> select concat('蘋果','香蕉','梨子');
+------------------------------+
| CONCAT('蘋果','香蕉','梨子') |
+------------------------------+
| 蘋果香蕉梨子                 |
+------------------------------+

案例二:出現 null 的情況

mysql> select concat('蘋果','香蕉',null);
+----------------------------+
| CONCAT('蘋果','香蕉',null) |
+----------------------------+
| NULL                       |
+----------------------------+

concat_ws()函式

1. 功能

concat_ws()函式功能和concat()一樣,將幾個字串拼接起來,只不過可以指定分隔符。

2. 語法

concat_ws(separator, str1, str2, ...)

3. 例子

案例1:將水果連線起來,並通過逗號分隔

mysql> select concat_ws(',','蘋果','香蕉','梨子');
+-------------------------------------+
| concat_ws(',','蘋果','香蕉','梨子') |
+-------------------------------------+
| 蘋果,香蕉,梨子                      |
+-------------------------------------+

案例2:引數出現 null 時,則忽略該引數

mysql> select concat_ws(',','蘋果',null,'梨子');
+-----------------------------------+
| concat_ws(',','蘋果',null,'梨子') |
+-----------------------------------+
| 蘋果,梨子                         |
+-----------------------------------+

案例3:如果引數全部為 null , 則返回空字串

mysql> select concat_ws(',',null,null,null);
+-------------------------------+
| concat_ws(',',null,null,null) |
+-------------------------------+
|                               |
+-------------------------------+

案例4:如果分隔符為 null ,則結果為 null

mysql> select concat_ws(null,'蘋果','香蕉','梨子');
+--------------------------------------+
| concat_ws(null,'蘋果','香蕉','梨子') |
+--------------------------------------+
| NULL                                 |
+--------------------------------------+

group_concat()函式

1. 功能

將group by產生的同一個分組中的值連線起來,返回一個字串結果。

2. 語法

group_concat( [distinct] 要連線的欄位 [order by 排序欄位 asc/desc ] [separator '分隔符'] )

說明:通過使用distinct可以排除重複值;如果希望對結果中的值進行排序,可以使用order by子句;separator是一個字串值,預設為一個逗號。

3. 例子

準備資料 ,這裡我們六年級故意多弄一個,為了方便後面測試

insert into table1 (study_section , grade ) values 
('小學','一年級'),
('小學','二年級'),
('小學','三年級'),
('小學','四年級'),
('小學','五年級'),
('小學','六年級'),
('小學','六年級'),
('初中','初一'),
('初中','初二'),
('初中','初三'),
('高中','高一'),
('高中','高二'),
('高中','高三');

案例一:根據學段獲取每個學段的年級

select study_section,group_concat(grade) from table1 group by study_section;

最後執行結果:

案例二:去除重的年級

select study_section,group_concat(distinct grade) from table1 group by study_section;

最後執行效果

案例三:指定排序順序

按照年級進行升序排列

mysql> select study_section,group_concat(distinct grade order by grade asc) from table1 group by study_section;
+---------------+-------------------------------------------------+
| study_section | group_concat(distinct grade order by grade asc) |
+---------------+-------------------------------------------------+
| 初中          | 初一,初三,初二                                  |
| 小學          | 一年級,三年級,二年級,五年級,六年級,四年級       |
| 高中          | 高一,高三,高二                                  |
+---------------+-------------------------------------------------+

按照年級進行降序排列

mysql> select study_section,group_concat(distinct grade order by grade desc) from table1 group by study_section;
+---------------+--------------------------------------------------+
| study_section | group_concat(distinct grade order by grade desc) |
+---------------+--------------------------------------------------+
| 初中          | 初二,初三,初一                                   |
| 小學          | 四年級,六年級,五年級,二年級,三年級,一年級        |
| 高中          | 高二,高三,高一                                   |
+---------------+--------------------------------------------------+

案例四:指定分割符號
預設用逗號分隔,這裡指定用分號進行分隔

mysql> select study_section,group_concat(distinct grade order by grade separator ';') from table1 group by study_section;
+---------------+-----------------------------------------------------------+
| study_section | group_concat(distinct grade order by grade separator ';') |
+---------------+-----------------------------------------------------------+
| 初中          | 初一;初三;初二                                            |
| 小學          | 一年級;三年級;二年級;五年級;六年級;四年級                 |
| 高中          | 高一;高三;高二                                            |
+---------------+-----------------------------------------------------------+