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