MySQL中group concat 函式的使用

2021-08-02 15:31:39 字數 1342 閱讀 3008

本文通過例項介紹了mysql中的group_concat函式的使用方法,比如select group_concat(name)。mysql中group_concat函式完整的語法如下:group_concat([distinct] 要連線的字段 [orderby 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(nameseparator ';') 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 byid;

| id| group_concat(distinct name) |

|1 | 10,20|

|2 | 20  |

|3 | 200,500 |

3 rows in set (0.00 sec)

以id分組,把name欄位的值列印在一行,逗號分隔,以name排倒序 mysql> selectid,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)

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...

MySQL 多行資料合併 GROUP CONCAT

表結構及資料 drop table if exists course create table course id int 11 not null,name varchar 50 default null comment 姓名 course name varchar 50 default null ...

MySQL 多行資料合併 GROUP CONCAT

表結構及資料 drop table if exists course create table course name varchar 255 character set utf8 collate utf8 general ci default null,course name varchar 25...