之前一直写代码递归调用实现数据库里面的具有父子节点的查询,
今天看到oracle中也支持这种查询,就收集了下,
首先感谢原博主分享:
原文地址:
http://blog.csdn.net/zhangzeyuaaa/article/details/52303698
有如下表Tree:
现在要递归查询冶炼(id=2)的所有父节点和子节点:
- SELECT * FROM tree START WITH id = 2 CONNECT BY PRIOR pid = id
- union
- SELECT * FROM tree START WITH id = 2 CONNECT BY pid = PRIOR id;
查询结果:
当然,也可以使用in关键字递归查询多个节点的父节点和子节点:
- SELECT * FROM tree START WITH id in (1, 10) CONNECT BY PRIOR pid = id
- union
- SELECT * FROM tree START WITH id in (1, 10) CONNECT BY pid = PRIOR id;
以上语句递归查询id为1或者10的父节点和子节点,结果如下: