嘿嘿, 我需要在不使用子查询的情况下重写以下代码:
SELECT country, region, name
FROM wcities
WHERE( country, region ) NOT IN ( SELECT country, code FROM regions )
AND country = 'IT'
AND region is NOT NULL;
我尝试过:
select c.country, c.region, c.name
from wcities c
join regions r on c.country <> r.country AND c.region <> r.code
where c.country = 'IT'
AND c.region is NOT NULL;
我明白为什么这不起作用,但我看不到我需要做什么。
提前致谢:)
答案 0 :(得分:2)
作为 left join
这将是:
SELECT c.country, c.region, c.name
FROM wcities c LEFT JOIN
regions r
ON r.country = c.country and r.region = c.region
WHERE r.country IS NULL AND
c.country = 'IT' AND
c.region is NOT NULL;
也就是说,我会为此目的推荐NOT EXISTS
:
SELECT c.country, c.region, c.name
FROM wcities c
WHERE NOT EXISTS (SELECT 1
FROM regions r
WHERE r.country = c.country and r.region = c.region
) AND
c.country = 'IT' AND
c.region is NOT NULL;