mysql中的group_concat函數的用法

來源:互聯網
上載者:User

標籤:

 1 本文通過執行個體介紹了MySQL中的group_concat函數的使用方法,比如select group_concat(name) 。  2 MySQL中group_concat函數  3 完整的文法如下:  4 group_concat([DISTINCT] 要串連的欄位 [Order BY ASC/DESC 排序欄位] [Separator ‘分隔字元‘])  5 基本查詢  6 mysql> select * from aa;  7 +------+------+  8 | id| name |  9 +------+------+ 10 |1 | 10| 11 |1 | 20| 12 |1 | 20| 13 |2 | 20| 14 |3 | 200 | 15 |3 | 500 | 16 +------+------+ 17 6 rows in set (0.00 sec) 18 以id分組,把name欄位的值列印在一行,逗號分隔(預設) 19 mysql> select id,group_concat(name) from aa group by id; 20 +------+--------------------+ 21 | id| group_concat(name) | 22 +------+--------------------+ 23 |1 | 10,20,20| 24 |2 | 20 | 25 |3 | 200,500| 26 +------+--------------------+ 27 3 rows in set (0.00 sec) 28 以id分組,把name欄位的值列印在一行,分號分隔 29 mysql> select id,group_concat(name separator ‘;‘) from aa group by id; 30 +------+----------------------------------+ 31 | id| group_concat(name separator ‘;‘) | 32 +------+----------------------------------+ 33 |1 | 10;20;20 | 34 |2 | 20| 35 |3 | 200;500 | 36 +------+----------------------------------+ 37 3 rows in set (0.00 sec) 38 以id分組,把去冗餘的name欄位的值列印在一行, 39 逗號分隔 40 mysql> select id,group_concat(distinct name) from aa group by id; 41 +------+-----------------------------+ 42 | id| group_concat(distinct name) | 43 +------+-----------------------------+ 44 |1 | 10,20| 45 |2 | 20 | 46 |3 | 200,500 | 47 +------+-----------------------------+ 48 3 rows in set (0.00 sec) 49 以id分組,把name欄位的值列印在一行,逗號分隔,以name排倒序 50 mysql> select id,group_concat(name order by name desc) from aa group by id; 51 +------+---------------------------------------+ 52 | id| group_concat(name order by name desc) | 53 +------+---------------------------------------+ 54 |1 | 20,20,10 | 55 |2 | 20| 56 |3 | 500,200| 57 +------+---------------------------------------+ 58 3 rows in set (0.00 sec) 59 使用group_concat_max_len系統變數,你可以設定允許的最大長度。 程式中進行這項操作的文法如下,其中 val 是一個不帶正負號的整數: 60 SET [SESSION | GLOBAL] group_concat_max_len = val; 61 若已經設定了最大長度, 則結果被截至這個最大長度。 62 將環境變數group_concat_max_len 增大。預設是1024.我就設定了session級的環境變數將其變為2048(不夠用再加大)

 

mysql中的group_concat函數的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.