我有两张桌子
一个是声明表,另一个是编辑
我正在加入ClaimID
我只想要具有205编辑但不是913的ClaimID。我需要为连接做些什么?
所以让我们说Edits是e而我正在检查e.RuleID可以有205,但不是913(每个claimid可以有无限制的编辑)
答案 0 :(得分:1)
SELECT * FROM Claims AS c
WHERE EXISTS (
SELECT * FROM Edits AS e1
WHERE c.ClaimID = e1.ClaimID
AND e1.RuleId=205)
AND NOT EXISTS (
SELECT * FROM Edits AS e2
WHERE c.ClaimID = e2.ClaimID
AND e2.RuleId=913)
答案 1 :(得分:0)
SELECT Edit205.ClaimID
FROM Edits Edit205
LEFT JOIN Edits Edit913
ON Edit205.ClaimID = Edit913.ClaimID AND Edit913.RuleID = 913
WHERE Edit205.RuleID = 205
AND Edit913.ClaimID IS NULL
工作sqlfiddle here
答案 2 :(得分:0)
这样做:
SELECT *
FROM Claims C
WHERE NOT EXISTS(SELECT 1
FROM Edits
WHERE Edit = 913 AND ClaimId = C.ClaimId)
AND EXISTS (SELECT 1
FROM Edits
WHERE Edit = 205 AND ClaimId = C.ClaimId)