我有一個包含原始記錄和取消記錄的表。原始記錄用代碼= 1標記,取消的記錄用代碼= 2標記。還有其他類型的代碼。我需要找到表中沒有取消記錄的所有原始記錄。如何在SQL中查找對
E.g.表1
id | column2 | column3 | code
-- |-------- | ------- | ---
1 | abc | def | 1
2 | xyz | pqr | 1
3 | abc | def | 2
查詢的結果應該是id爲2的行,因爲它尚未取消。
我想使用自聯接
select *
from table1 t1
join table1 t2 on t1.column1=t2.column1
and t1.column2=t2.column2
and t1.code<t2.code
where not (t1.code=1 and t2.code=2)
@ AT-2016 AFAIK取消取消活動記錄,所以它不應出現在結果集中。 –
由於第1行在第3行中有取消記錄。在此表中,插入了一條新記錄用於取消。 – Jansvin
@Tim明白了。謝謝。 –