1

我有这张表 [表 1]

西德 | 到了| 到达日期

[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
4

2 回答 2

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

于 2012-06-19T09:47:35.353 回答
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
于 2012-06-19T09:49:00.983 回答