簡介
with...as...
需要定義一個sql片段,會將這個片段產生的結果集保存在內存中,
后續的sql均可以訪問這個結果集和,作用與視圖或臨時表類似.
語法說明
-
with...as...
必須和其他語句一起使用 -
with...as...
是一次性的
with...as...
的示例如下:
-- with table_name as(子查詢語句) 其他sql with tmp as ( select * from xxx ) select * from tmp;
同級的多個臨時表之間用,
as
后的子句必須用()
,
with tmp1 as ( select * from xxx ),tmp2 as ( select * from xxx ) select * from tmp1,tmp2;
with...as...
使用嵌套的例子:
with tmp2 as ( with tmp1 as ( select * from xxx ) select * from tmp1 ) select * from tmp2;
優點
- 提高代碼可讀性(結構清晰)
- 簡化sql,優化執行速度(
with
子句只需要執行一次)