5. SQL CTE
常用于优化子查询书写。使用with字句,将子查询字句提取出了,提高SQL可读性。
复杂点的操作,可以使用with字句写出递归查询
示例一
with
a as (
SELECT .... from xxx
),
b as (
SELECT .... from a
)
select ... from b
示例二
WITH RECURSIVE cte_count (n)
AS (
-- 递归起始
SELECT 1
UNION ALL
-- 递归操作
SELECT n + 1
FROM cte_count
-- 递归终止条件
WHERE n < 3
)
SELECT n
FROM cte_count;
Loading...