mysql中group_concat函式
完整的語法如下:
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)
注 如果要進行分組統計只需要count對應的字段即可
mysql中合併函式 MYSQL分組合併函式
mysql中group concat函式 完整的語法如下 group concat distinct 要連線的字段 order by asc desc 排序字段 separator 分隔符 基本表 id name 1 10 1 20 1 20 2 20 3 200 3 500 例1 以id分組,把n...
mysql中合併函式 MYSQL 分組合併函式
mysql中group concat函式 完整的語法如下 group concat distinct 要連線的字段 order by asc desc 排序字段 separator 分隔符 基本查詢 mysql select from aa id name 1 10 1 20 1 20 2 20 3...
Mysql分組資料合併group concat用法
group concat用法 預設用法 select group concat vaccine name as do accinename from t vaccine where id in select vaccine id from t dog vaccine where dog id 1 1...