MySQL Query将分组记录

时间:2012-06-19 09:41:28

标签: mysql

我有这张表[表1]

cid |到了| date_arrived

[到达的字段的值可以是[T]或[F],值是[F]日期到达的字段是NULL

1条记录最多可能只出现2条记录(已达到1条记录= T,另一条记录为已抵达= F)但也有记录可能只出现一次

1 | T | 2012-02-01
2 | F | [Null]
1 | F | [Null]
3 | T | 2012-03-05

我需要一个显示类似

的查询
cid | arrived | not_arrived
1      Yes          Yes
2      No           Yes
3      Yes          No

2 个答案:

答案 0 :(得分:2)

这有效:

SELECT
    cid,
    SUM(arrived = 'T') > 0 as arrived,
    SUM(arrived = 'F') > 0 as not_arrived 
FROM [Table 1]
GROUP BY cid;

您可以在此处试用:http://sqlfiddle.com/#!2/2b5a7/1/0

答案 1 :(得分:0)

select cid, 
   case when find_in_set('T', group_concat(arrived)) > 0 then 'yes'  else 'no' end as arrived, 
   case when find_in_set('F', group_concat(arrived)) > 0 then 'yes'  else 'no' end as not_arrived
from table1
group by cid