我正在尝试从Table1返回行,其中id在表2中存在1或3次。这个sql用于执行一次计数,但是当我将它包含多次以OR分隔时,它就会失败。
Table1 Table2
id color id
----- ----- -----
1 bl 1
2 r 4
3 g 1
4 y 2
1
2
SELECT d FROM Table1 d
WHERE d.id IN
( SELECT e.id FROM Table2 e
GROUP BY e.id
HAVING COUNT(e.id) = 1 OR HAVING COUNT(e.id) = 3
)
预期产出:
id color
--- -----
1 bl
4 y
答案 0 :(得分:0)
这样的事情可以解决问题:
SELECT d.* FROM
Table1 d,
( SELECT e.id id, COUNT(1) myCount FROM Table2 e
GROUP BY e.id
) t
WHERE d.id = t.id
AND (t.myCount = 1 OR t.myCount = 3)