将列作为分隔列表加入MySQL查询

将列作为分隔列表加入MySQL查询,mysql,sql,Mysql,Sql,我在MySQL中有一个表,看起来是这样的: date | name | status 03-13-2014,Bob Jones,Pending 03-13-2014,George Manuel,Pending 03-13-2014,Tom Grables,Pending 03-13-2014,Frankie Avalon,Approved 03-13-2014,Robert Garcia,Approved 03-14-2014,John Doe,Pending 03-14-201

我在MySQL中有一个表,看起来是这样的:

date      | name    | status
03-13-2014,Bob Jones,Pending
03-13-2014,George Manuel,Pending
03-13-2014,Tom Grables,Pending
03-13-2014,Frankie Avalon,Approved
03-13-2014,Robert Garcia,Approved
03-14-2014,John Doe,Pending
03-14-2014,John Die,Approved
03-14-2014,John Zoe,Approved
我要做的是抓取所有表示已批准的行的日期、名称和状态。但是,我还希望以这样一种方式加入这个查询,即我获取的每一行都有一个分号分隔的列表,其中包含那些状态为“待定”且与“已批准”状态相同的人的姓名。因此,我的查询输出如下所示:

03-13-2014,Frankie Avalon,Approved,Bob Jones;George Manuel;Tom Grables
03-13-2014,Robert Garcia,Approved,Bob Jones;George Manuel;Tom Grables
请注意,我抓取了2014年3月13日的已批准请求,并将状态为“待定”且日期与已批准请求匹配的请求添加为一列。我一直在搞乱join语句,也做了谷歌作业,但还没有找到一种方法来实现这一点

任何想法都将不胜感激。

仔细研究


我想这就是你想要的逻辑:

 select `date`,
         group_concat( (case when status = 'Approved' then name end) separator ';') as Approveds,
         group_concat( (case when status = 'Pending' then name end) separator ';') as Pendings
 from t
 group by `date`;
如果您确实希望仅在没有批准的日期挂起,则需要额外的筛选器:

 select `date`,
         group_concat( (case when status = 'Approved' then name end) separator ';') as Approveds,
         group_concat( (case when status = 'Pending' then name end) separator ';') as Pendings
 from t
 group by `date`
 having `date` in (select `date` from t where status = 'Approved')

如果未指定分隔符,“是分隔符。我指定一个是出于习惯。哇,非常感谢!我已经为此工作了几天,几分钟内我的问题就得到了回答。你们真是太棒了,我很感谢你们给我展示一些新的东西。
 select `date`,
         group_concat( (case when status = 'Approved' then name end) separator ';') as Approveds,
         group_concat( (case when status = 'Pending' then name end) separator ';') as Pendings
 from t
 group by `date`
 having `date` in (select `date` from t where status = 'Approved')