以下查询有效:
select count(*) from everything where num not in (select num from sometable)
以下查询应该与上面的查询等效,但会导致“无效标识符”错误:
with unwanted as (select num from sometable)
select count(*) from everything where num not in unwanted
第二个查询有什么问题?
语法是这样的:
with unwanted as (select num from sometable)
select count(*) from everything where num not in (select * from unwanted)
select num from sometable
显然,这只有在零件更复杂或以后多次使用时才有意义……
您还可以加入表以获得更快的性能
WITH unwanted
AS
(
SELECT num
FROM sometable
)
SELECT COUNT(*)
FROM everything a
LEFT JOIN unwanted b
ON a.num = b.num
WHERE b.num IS NULL