概述
实际生活有很多树形结构的数据,比如公司分为多个部门,部门下分为多个组,组下分为多个员工;省市县的归属;页面菜单栏等等。
如果想查询某个节点的父节点或者子节点,一般通过表自身连接完成,但如果该节点的子节点还有多层结构,就需要使用递归调用。但如果数据量特别大,递归的次数指数级上升,而且查询数据库的次数也指数级上升,导致程序和数据库压力剧增,查询时间特别长。那数据库有没有递归查询语句呢?答案是肯定的。
start with connect by prior 递归查询
1、数据准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
2 start with connect by prior递归查询
- start with 子句:遍历起始条件。如果要查父结点,这里可以用子结点的列,反之亦然。
- connect by 子句:连接条件。prior 跟父节点列parentid放在一起,就是往父结点方向遍历;prior 跟子结点列subid放在一起,则往叶子结点方向遍历。parent_id、id两列谁放在 “=” 前都无所谓,关键是prior跟谁在一起。
- order by 子句:排序。
常用的select项:
LEVEL:级别
connect_by_root:根节点
sys_connect_by_path:递归路径
2.1 查询所有子节点
1 2 3 4 |
|
其实,如果单层结构,使用表自身连接也可以实现:
1 2 |
|
当查询节点下有多层数据:
1 2 3 4 |
|
1 2 |
|
如果使用自身连接,也只能查到子一级节点的数据,需要遍历子一级节点,递归查询每个子一级节点下的子节点。明显麻烦很多!!!
2.2 查询所有父节点
1 2 3 4 5 |
|
2.3 查询指定节点的根节点
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 |
|
2.4 查询下行政组织递归路径
1 2 3 4 |
|
3 with递归查询
3.1 with递归子类
1 2 3 4 5 6 7 8 9 10 11 |
|
3.2 递归父类
1 2 3 4 5 6 7 8 9 10 11 12 |
|