as with 用法

来源:互联网 发布:淘宝升业绩 编辑:程序博客网 时间:2024/05/16 06:36

今天在网上查找优化的案例,碰巧在一个大神的博客里看到这个用法了,起初还很疑惑,所以就跟着做了下实验:
SQL> create table t2(id int);
Table created.

SQL> create table t1(id int);

Table created.

SQL> insert into t2 values(1);

1 row created.

SQL> insert into t2 values(2);

1 row created.

SQL> insert into t2 values(3);

1 row created.

SQL> insert into t1 values(1);

1 row created.
SQL> select * from t1;

        ID
----------
         1

SQL> select * from t2;

        ID
----------
         1
         2
         3
SQL> with a as (select * from t1),
  2   b as (select * from t2)
  3  select * fromt2
  4  union
  5  select * fromt1;

        ID
----------
         1
         2
         3

SQL> with a as (select * from t1),
  2  b as (select * from t2)
  3  select * froma
  4  union
  5  select * fromb;

        ID
----------
         1
         2
         3
SQL> with a as (select * from t1),
  2  b as (select * from t2)
  3  select * from a
  4  union
  5  select * from b
  6  where id in (2,3);

        ID
----------
         1
         2
         3

SQL> with a as (select * from t1),
  2  b as (select * from t2)
  3  select * from a
  4  union
  5  select * from b
  6  where id not in (2,3);

        ID
----------
         1
可以看出来where条件只是对每个SELECT语句的表做限制。