mysql之group_concat函數

mysql之group_concat函數

在介紹GROUP_CONCAT以前,咱們先來看看concat()函數和concat_ws()函數。mysql

先準備一個測試數據庫:sql

mysql> select * from scores;
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  1 | zhangsan | 100   |
|  2 | lisi     | 90    |
|  3 | wangwu   | 99    |
|  4 | zhangsan | 92    |
|  5 | zhangsan | 88    |
|  6 | lisi     | 89    |
+----+----------+-------+
6 rows in set

concat()函數

concat()函數的功能是將多個字符鏈接成一個字符串。數據庫

語法: concat(str1, str2,...)segmentfault

返回結果爲鏈接參數產生的字符串,若是有任何一個參數爲null,則返回值爲null。函數

mysql> select *,concat(name,score) from scores;
+----+----------+-------+--------------------+
| id | name     | score | concat(name,score) |
+----+----------+-------+--------------------+
|  1 | zhangsan | 100   | zhangsan100        |
|  2 | lisi     | 90    | lisi90             |
|  3 | wangwu   | 99    | wangwu99           |
|  4 | zhangsan | 92    | zhangsan92         |
|  5 | zhangsan | 88    | zhangsan88         |
|  6 | lisi     | 89    | lisi89             |
+----+----------+-------+--------------------+
6 rows in set
-- 加分隔符 & 起別名
mysql> select *,concat(name,':',score) as info from scores;
+----+----------+-------+--------------+
| id | name     | score | info         |
+----+----------+-------+--------------+
|  1 | zhangsan | 100   | zhangsan:100 |
|  2 | lisi     | 90    | lisi:90      |
|  3 | wangwu   | 99    | wangwu:99    |
|  4 | zhangsan | 92    | zhangsan:92  |
|  5 | zhangsan | 88    | zhangsan:88  |
|  6 | lisi     | 89    | lisi:89      |
+----+----------+-------+--------------+
6 rows in set

concat_ws()函數

concat()函數加分隔符比較麻煩,若是有10個字段鏈接起來,就得寫9個分隔符,concat_ws()函數就是爲了解決這個問題。concat_ws就是concat with separator。測試

語法: concat_ws(separator, str1, str2, ...)url

第一個參數指定分隔符。須要注意的是分隔符不能爲null,若是爲null,則返回結果爲null。.net

mysql> select *,concat_ws(':',name,score) as info from scores;
+----+----------+-------+--------------+
| id | name     | score | info         |
+----+----------+-------+--------------+
|  1 | zhangsan | 100   | zhangsan:100 |
|  2 | lisi     | 90    | lisi:90      |
|  3 | wangwu   | 99    | wangwu:99    |
|  4 | zhangsan | 92    | zhangsan:92  |
|  5 | zhangsan | 88    | zhangsan:88  |
|  6 | lisi     | 89    | lisi:89      |
+----+----------+-------+--------------+
6 rows in set

group_concat函數

明白了concat()和concat_ws()函數,咱們來看一下GROUP_CONCAT()函數。它的功能就是將group by產生的同一個分組中的值鏈接起來,返回一個字符串結果。若是單獨使用,那麼就將指定字段全部的值鏈接起來。code

語法:blog

group_concat( [distinct] 要鏈接的字段 [order by 排序字段 asc/desc ][separator '分隔符'] )

說明:經過使用distinct能夠排除重複值;若是但願對結果中的值進行排序,能夠使用order by子句;separator是一個字符串值,缺省爲一個逗號。

咱們知道能夠使用group by語句對結果進行分組處理:

mysql> select * from scores group by name;
+----+----------+-------+
| id | name     | score |
+----+----------+-------+
|  2 | lisi     | 90    |
|  3 | wangwu   | 99    |
|  1 | zhangsan | 100   |
+----+----------+-------+
3 rows in set

可是咱們只能看到zhangsan的第一個成績,若是我想看到全部的成績呢?

mysql> select *,group_concat(score) from scores group by name;
+----+----------+-------+---------------------+
| id | name     | score | group_concat(score) |
+----+----------+-------+---------------------+
|  2 | lisi     | 90    | 90,89               |
|  3 | wangwu   | 99    | 99                  |
|  1 | zhangsan | 100   | 100,92,88           |
+----+----------+-------+---------------------+
3 rows in set

-- 將分組結果按升序排序,並使用分隔符 :
mysql> select *,group_concat(score order by score separator ':') from scores group by name;
+----+----------+-------+--------------------------------------------------+
| id | name     | score | group_concat(score order by score separator ':') |
+----+----------+-------+--------------------------------------------------+
|  2 | lisi     |    90 | 89:90                                            |
|  3 | wangwu   |    99 | 99                                               |
|  1 | zhangsan |   100 | 88:92:100                                        |
+----+----------+-------+--------------------------------------------------+
3 rows in set

-- 上面展現了以name分組後全部的score,如今多加展現一個id
mysql> select *,group_concat(concat_ws(':',id,score) order by id) from scores group by name;
+----+----------+-------+---------------------------------------------------+
| id | name     | score | group_concat(concat_ws(':',id,score) order by id) |
+----+----------+-------+---------------------------------------------------+
|  2 | lisi     |    90 | 2:90,6:89                                         |
|  3 | wangwu   |    99 | 3:99                                              |
|  1 | zhangsan |   100 | 1:100,4:92,5:88                                   |
+----+----------+-------+---------------------------------------------------+
3 rows in set

-- 單獨使用
mysql> select group_concat(score) from scores;
+---------------------+
| group_concat(score) |
+---------------------+
| 100,90,99,92,88,89  |
+---------------------+
1 row in set
參考:

https://blog.csdn.net/mary199...

相關文章
相關標籤/搜索