1.作用
with 。。as需要定义一个sql片段,会将这个片段产生的结果集保存在内存中,后续的sql均可以访问这个结果集,作用与视图或临时表类似.
2语法
with...as...必须和其他sql一起使用(可以定义一个with但在后续语句中不使用他)with...as...是一次性的,是临时的
3.用法
1.可以单独使用
-- with table_name as(子查询语句) 其他sql
with temp as (
select * from xxx
)
select * from temp;
2.嵌套连续使用
with temp2 as (
with temp1 as (
select * from xxx
)
select * from temp1
)
select * from temp2;
3.可以当 join…on 使用
with temp1 as (
select * from xxx
),temp2 as (
select * from xxx
)
select * from temp1,temp2;
一般来说:
表少用join…on
表多用with…as
浙公网安备 33010602011771号