PostgreSQL-With子句实现递归
上手:
|
sql 实现 菜单树展示:
WITH RECURSIVE tree AS ( |
该语句是从子部门(id
= 7)向上查询,即查询该部门的父部门。同样的我们也可以修改第二行后数值 ”7“ 来查看不同的结果。如下:
|
注意:
语法
with recursive 名字 as (
A.初始语句(非递归部分)
union all
B.递归部分语句
) [SELECT | INSERT | UPDATE | DELETE]
可以到 WITH RECURSIVE
与普通的 WITH
相比,最大的不同就是 as ()
中的 的内容。内容被 union all
一分为二,前半部分 A 为非递归语句,后半部分 B 为要进行的递归语句。
执行步骤如下
- 执行 A 部分。(如果使用的是union而非union all,则需对结果去重)其结果作为 B 中对result的引用,同时将这部分结果放入临时的working table中
- 重复执行如下步骤,直到working table为空:用working table的内容替换递归的自引用,执行 B ,(如果使用union而非union all,去除重复数据),并用该结果(如果使用union而非union all,则是去重后的结果)替换working table
注意项
- 初始语句 A 中的列与递归部分语句 B 的列必须要一一对应
- 初始语句 A 、递归部门语句 B 查询的时候不要使用
*
号来代替列 - 一定要有结束条件,否则会进入死循环
我是个双鱼座的小王子,沉浸在自己的代码世界里,去探索这未知的世界,希望遇到更多的小伙伴一起前行!