SQL Navigator中的“with ... as”

时间:2012-10-02 14:37:19

标签: sql oracle

以下查询有效:

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

第二个查询有什么问题?

2 个答案:

答案 0 :(得分:7)

语法是这样的:

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

显然只有select num from sometable部分稍微复杂一点或以后使用过几次才有意义......

答案 1 :(得分: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