2012-06-19 106 views
1

我有这张表[表1]MySQL查询,将组记录

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

这工作记录:

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

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