我有一个像下面这样的表,其中记录基本上是两对(相同的ID1和ID2)但注意和状态不同(有时)。 我需要选择所有同时具有等于"准备好QC"字段的记录。并且字段Status等于1,并且具有字段Note的记录等于"准备编目"状态为0。
ID1, ID2, Note, Status
3, 22, Ready for QC, 1
3, 22, Ready for Cataloging, 0
36, 22, Ready for QC, 1
36, 22, Ready for Cataloging, 1
63, 22, Ready for QC, 1
63, 22, Ready for Cataloging, 0
67, 67, Ready for QC, 0
67, 67, Ready for Cataloging, 0
有什么建议吗?谢谢。乔瓦尼
答案 0 :(得分:4)
我认为你想要这两个条件存在的所有记录的ID1和ID2。
它假定每个ID1,ID2,Note,Status的唯一性(意味着表中不能有两个相同的记录)
SELECT ID1, ID2, count(*) cnt
FROM Table
WHERE (Note = "Ready to QC" and status = 1)
OR(note = "Ready for Cataloging" and Status 0)
GROUP BY ID1, ID2
HAVING COUNT(*) = 2
答案 1 :(得分:0)
这个更长但是重复:
SELECT t1.id1, t1.id2, t1.note, t1.status
FROM
(SELECT *
FROM t
WHERE note = 'Ready for QC'
AND status = 1) t1
INNER JOIN
(SELECT *
FROM t
WHERE note = 'Ready for Cataloging'
AND status = 0) t2
ON t1.id1 = t2.id1
AND t1.id2 = t2.id2