Home  >  Article  >  Database  >  mysql的group_concat函数使用示例_MySQL

mysql的group_concat函数使用示例_MySQL

WBOY
WBOYOriginal
2016-06-01 13:17:41952browse

MySQL中group_concat函数,完整的语法如下:

group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])

基本查询


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字段的值打印在一行,逗号分隔(默认)


select id,group_concat(name) from aa group by id;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn