WITH AS语句,也称为子查询一部分(subquery factoring),能够 使你做许多事儿,界定一个SQL片段,该SQL片段会被全部SQL语句常用到。有的情况下,是为了更好地让SQL语句的易读性更高些,也是有很有可能是在UNION ALL的不一样一部分,做为给出的数据的一部分。
with as 用法
–对于一个别称
with tmp as (select * from tb_name)
–对于好几个别称
with
tmp as (select * from tb_name),
tmp2 as (select * from tb_name2),
tmp3 as (select * from tb_name3),
…
–相当于建了个e临时表
with e as (select * from scott.emp e where e.empno=7499)
select * from e;
–相当于建了e、d临时表
with
e as (select * from scott.emp),
d as (select * from scott.dept)
select * from e, d where e.deptno = d.deptno;