是的,我们可以使用MySQL GROUP_CONCAT()将列记录转换为列表。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> ClientId int, -> ClientName varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(100,'Chris'); mysql> insert into DemoTable values(100,'Robert'); mysql> insert into DemoTable values(100,'Adam'); mysql> insert into DemoTable values(100,'David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 100 | Chris | | 100 | Robert | | 100 | Adam | | 100 | David | +----------+------------+ 4 rows in set (0.00 sec)
这是将列记录转换为列表的查询-
mysql> select ClientId,group_concat(ClientName separator ',') from DemoTable group by ClientId;
这将产生以下输出-
+----------+----------------------------------------+ | ClientId | group_concat(ClientName separator ',') | +----------+----------------------------------------+ | 100 | Chris,Robert,Adam,David | +----------+----------------------------------------+ 1 row in set (0.00 sec)