6

以下查询有效:

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

第二个查询有什么问题?

4

2 回答 2

7

语法是这样的:

with unwanted as (select num from sometable)
select count(*) from everything where num not in (select * from unwanted)

select num from sometable显然,这只有在零件更复杂或以后多次使用时才有意义……

于 2012-10-02T14:40:18.257 回答
2

您还可以加入表以获得更快的性能

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
于 2012-10-02T14:44:40.350 回答