为此,您可以使用group_concat()。让我们首先创建一个表-
mysql> create table DemoTable1507 -> ( -> Name varchar(20), -> PaperSet int -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1507 values('Chris',111); mysql> insert into DemoTable1507 values('David',112); mysql> insert into DemoTable1507 values('Mike',111); mysql> insert into DemoTable1507 values('Bob',113);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1507;
这将产生以下输出-
+-------+----------+ | Name | PaperSet | +-------+----------+ | Chris | 111 | | David | 112 | | Mike | 111 | | Bob | 113 | +-------+----------+ 4 rows in set (0.00 sec)
这是使MySQL仅在一行中显示结果的查询-
mysql> select group_concat(Name order by Name desc separator ':') -> from DemoTable1507 -> where PaperSet=111;
这将产生以下输出-
+-----------------------------------------------------+ | group_concat(Name order by Name desc separator ':') | +-----------------------------------------------------+ | Mike:Chris | +-----------------------------------------------------+ 1 row in set (0.00 sec)