MySQL中g(shù)roup_concat函數(shù)深入理解
更新時間:2012年11月14日 16:28:43 作者:
本文通過實例介紹了MySQL中的group_concat函數(shù)的使用方法,需要的朋友可以適當參考下
本文通過實例介紹了MySQL中的group_concat函數(shù)的使用方法,比如select group_concat(name) 。
MySQL中g(shù)roup_concat函數(shù)
完整的語法如下:
group_concat([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
基本查詢
mysql> select * from aa;
+------+------+
| 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_max_len系統(tǒng)變量,你可以設(shè)置允許的最大長度。 程序中進行這項操作的語法如下,其中 val 是一個無符號整數(shù):
SET [SESSION | GLOBAL] group_concat_max_len = val;
若已經(jīng)設(shè)置了最大長度, 則結(jié)果被截至這個最大長度。
將環(huán)境變量group_concat_max_len 增大。默認是1024.我就設(shè)置了session級的環(huán)境變量將其變?yōu)?048(不夠用再加大)。解決該問題
MySQL中g(shù)roup_concat函數(shù)
完整的語法如下:
group_concat([DISTINCT] 要連接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
基本查詢
mysql> select * from aa;
+------+------+
| 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_max_len系統(tǒng)變量,你可以設(shè)置允許的最大長度。 程序中進行這項操作的語法如下,其中 val 是一個無符號整數(shù):
SET [SESSION | GLOBAL] group_concat_max_len = val;
若已經(jīng)設(shè)置了最大長度, 則結(jié)果被截至這個最大長度。
將環(huán)境變量group_concat_max_len 增大。默認是1024.我就設(shè)置了session級的環(huán)境變量將其變?yōu)?048(不夠用再加大)。解決該問題
相關(guān)文章
mysql備份恢復(fù)mysqldump.exe幾個常用用例
收集了,一個整理不錯的,mysql備份與恢復(fù)用法2008-08-08Mysql實現(xiàn)Oracle中的Start with...Connect by方式
文章總結(jié):作者在遷移數(shù)據(jù)庫時,使用了Oracle的startwith進行樹的遞歸查詢,但遇到了一些問題,通過搜索和修改,作者成功地使用存儲過程和預(yù)處理語句來實現(xiàn)動態(tài)查詢,并解決了變量聲明和預(yù)處理語句的問題2024-12-12