为此,请使用GROUP_CONCAT()。对于只有1个值,请使用MySQL WHERE子句。让我们首先创建一个表-
create table DemoTable ( PlayerName varchar(40), PlayerStatus tinyint(1) );
使用插入命令在表中插入一些记录-
insert into DemoTable values('Chris',1); insert into DemoTable values('David',0); insert into DemoTable values('Sam',1); insert into DemoTable values('Carol',1); insert into DemoTable values('Bob',0);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+------------+--------------+ | PlayerName | PlayerStatus | +------------+--------------+ | Chris | 1 | | David | 0 | | Sam | 1 | | Carol | 1 | | Bob | 0 | +------------+--------------+ 5 rows in set (0.00 sec)
以下是对concat进行分组并根据相应列中的1个值将数据放入一行的查询-
select group_concat(PlayerName) from DemoTable where PlayerStatus=1;
这将产生以下输出-
+--------------------------+ | group_concat(PlayerName) | +--------------------------+ | Chris,Sam,Carol | +--------------------------+ 1 row in set (0.00 sec)