当查询中多次用到某一部分时,可以用Oracle with语句创建一个公共临时表。因为子查询在内存临时表中,避免了重复解析,所以执行效率会提高不少。临时表在一次查询结束自动清除。
一般语法格式:
复制代码代码如下:
with
alias_name1 as (subquery1),
alias_name2 as (subQuery2),
…… alias_nameN as (subQueryN)
select col1,col2…… col3
from alias_name1,alias_name2……,alias_nameN
Oracle with语句的例子:
复制代码代码如下:
WITH Q1 AS (SELECT 3 + 5 S FROM DUAL),
Q2 AS (SELECT 3 * 5 M FROM DUAL),
Q3 AS (SELECT S, M, S + M, S * M FROM Q1, Q2)
SELECT * FROM Q3;
输出结果:
复制代码代码如下:
1 8 15 23 120