2012-06-19 140 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