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 ';') |
+---------------+-----------------------------------------------------------+
| 初中 | 初一;初三;初二 |
| 小學 | 一年級;三年級;二年級;五年級;六年級;四年級 |
| 高中 | 高一;高三;高二 |
+---------------+-----------------------------------------------------------+