根据原因查找数据列表

时间:2017-10-26 21:20:14

标签: mysql sql database

我的数据集低于数据集。我需要找到ID已“创建”但未“删除”的列表

TimeStamp   ID  Reason
2:38:46     1   created
2:39:46     2   Created
2:40:46     1   Removed 
2:42:46     3   Created 
2:45:46     2   Updated
2:48:46     4   Created
2:45:46     3   Removed

结果应该是针对上述数据集:

2:39:46     2   Created
2:48:46     4   Created

1 个答案:

答案 0 :(得分:1)

脑海中浮现出

not exists

select t.*
from t
where t.reason = 'Created' and
      not exists (select 1 from t t2 where t2.id = t.id and t2.reason = 'Removed');