我有2个表,你可以看到我在下面提到的,CaseID
与主键和外键有关,所以我想要的结果是其行动为1而在第二个表队列中不等于7,但是我的结果是给了我2条记录,根据我的条件记录应该是一条,它的返回只是CaseID 3.所以你能不能在我的查询中出错。
CaseID action
1 1
2 0
3 1
4 0
Cid CaseID Queue
1 1 1
2 1 2
3 1 7
4 3 1
5 3 2
6 4 3
7 4 6
8 2 4
9 2 5
查询:
select
CaseTblA.CaseID
from
CaseTblA
inner join
CaseTblB on CaseTblA.CaseID = CaseTblB.CaseID
where
CaseTblA.Action = 1 and CaseTblB.Queue <> 7
group by
CaseTblA.CaseID
此查询返回:
CaseID
1
3
但它应该是返回:
CaseID
3
答案 0 :(得分:1)
试试这个:
SELECT CaseTblA.CaseID
FROM CaseTblB INNER JOIN
CaseTblA ON CaseTblB.CaseID = CaseTblA.CaseID
INNER JOIN
(select CaseID, MAX(Queue) as maxqueue
from CaseTblB
group by CaseID
Having MAX(Queue) <> 7) a on
a.CaseID = CaseTblA.CaseID
where CaseTblA.Action = 1
GROUP BY CaseTblA.CaseID