with as语法 (sql递归)
–针对一个别名
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),
…
ag:
--相当于建了个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;
ag:
with cte_parent(ORG_CODE,ORG_CNAME_F,SUP_ORG_CODE)
as (
select a.ORG_CODE , a.ORG_CNAME_F,a.SUP_ORG_CODE from org a where a.org_number like '%1000%' and is_enable=1
union all
select a.ORG_CODE , a.ORG_CNAME_F,a.SUP_ORG_CODE from org a inner join cte_parent b on a.ORG_CODE=b.SUP_ORG_CODE where is_enable=1
)
select distinct a.* from cte_parent a