Oracle实现递归查询

前端之家收集整理的这篇文章主要介绍了Oracle实现递归查询前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有如下表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的父节点和子节点,结果如下:

原文链接:https://www.f2er.com/oracle/212930.html

猜你在找的Oracle相关文章