您可以将group_concat()与concat()
并置以连接单个列的所有值。让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(FirstName) values('John'); mysql> insert into DemoTable(FirstName) values('Larry'); mysql> insert into DemoTable(FirstName) values('Chris'); mysql> insert into DemoTable(FirstName) values('Robert'); mysql> insert into DemoTable(FirstName) values('David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Larry | | 3 | Chris | | 4 | Robert | | 5 | David | +----+-----------+ 5 rows in set (0.00 sec)
以下是连接MySQL中单个列的所有值的查询。
mysql> select group_concat(concat('"', FirstName, '"')) AS FirstName from DemoTable;
这将产生以下输出-
+-----------------------------------------+ | FirstName | +-----------------------------------------+ | "John","Larry","Chris","Robert","David" | +-----------------------------------------+ 1 row in set (0.05 sec)