让我们首先创建一个表-
create table DemoTable ( Id int, Name varchar(100) );
使用插入命令在表中插入一些记录-
insert into DemoTable values(100,'Chris'); insert into DemoTable values(200,'Robert'); insert into DemoTable values(100,'Chris'); insert into DemoTable values(300,'David'); insert into DemoTable values(400,'Mike'); insert into DemoTable values(400,'Mike');
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+------+--------+ | Id | Name | +------+--------+ | 100 | Chris | | 200 | Robert | | 100 | Chris | | 300 | David | | 400 | Mike | | 400 | Mike | +------+--------+ 6 rows in set (0.00 sec)
以下是对ID为不同的MySQL组Concat的查询-
select group_concat(Name) from ( select distinct Id,Name from DemoTable ) tbl;
这将产生以下输出-
+-------------------------+ | group_concat(Name) | +-------------------------+ | Chris,Robert,David,Mike | +-------------------------+ 1 row in set (0.00 sec)