内联视图、标量子查询、WITH子查询示例

with t as (select * from scott.emp where 1=1)
select * from t where empno='';

select * from (
select sal as salary,comm as commission from scott.emp ) x
where salary <5000;


-- 内联视图
内联视图通常是指 一个SQL查询的结果作为另一个查询的数据源, 一般在 From字句后面
select p.pname,c1_sum1,c2_sum2
from p,
(select id,sum(q1) c1_sum1 from s1 group by id) s1,
(select id,sum(q2) c2_sum2 from s2 group by id) s2
where p.id=s1.id
and p.id=s2.id

-- 标量子查询
select
p.pname,
(select sum(q1) c1_sum1 from s1 where s1.id=p.id) c1_sum1,
(select sum(q2) c1_sum1 from s2 where s2.id=p.id) c2_sum2
from p

-- with子查询分解
主要是方便理解,增加可读性
with c1_vw as (select id,sum(q1) sum1 from s1 group by id),
c2_vw as (select id,sum(q2) sum2 from s2 group by id),
c1_c2 as (select t1.id,t1.sum1,t2.sum2 from c1_vw t1,c2_vw t2 where t1.id=t2.id)
select p.pname,sum1,sum2
from p,c1_c2
where p.id=c1_c2.id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值