我有子查询的多个IN条件。
SELECT
S.name,
S.email
FROM something S
WHERE
1 NOT IN (SELECT id FROM tags WHERE somethingId = S.id)
AND 2 NOT IN (SELECT id FROM tags WHERE somethingId = S.id)
AND 3 NOT IN (SELECT id FROM tags WHERE somethingId = S.id)
也许有更好的解决方案?类似的东西:
(1, 2, 3) NOT IN (SELECT id FROM tags WHERE somethingId = S.id)
答案 0 :(得分:4)
重写以改为使用NOT EXISTS
。即,当标签中没有行,其中等于s.id并且id为1,2或3时,从S返回。
SELECT
S.name,
S.email
FROM something S
WHERE NOT EXISTS (SELECT 1 FROM tags WHERE id in (1, 2, 3) and somethingId = S.id)
NOT EXISTS
也是“无效的”。 (NOT IN (select returning a null)
根本不会返回任何行。)