在Oracle中的树形操作
1.取子节点及路径(正树):
select t.id ,t.code, t.name ,t.pid
,SYS_CONNECT_BY_PATH(t.id,'.')||'.' as IdPath
from tas_catalog t
--where id!=110
start with id=110
connect by pid = prior id
order siblings by id
2.取各级父节点(倒树):
select t.id ,t.code, t.name ,t.pid
,SYS_CONNECT_BY_PATH(t.id,'.')||'.' as IdPath --路径从反的
from tas_catalog t
--where id!=110
start with id=110
connect by id = prior pid
order siblings by id
无论正树还是倒树, 关键就在于connect by的条件.
正树: 必须是 ‘父’= prior ‘子’
倒树: 必须是 ‘子’= prior ‘父’
本文介绍了在Oracle数据库中进行树形结构查询的方法,包括如何获取子节点及其路径(正树查询)以及如何获取各层级的父节点(倒树查询)。通过调整`CONNECT BY`条件可以实现这两种不同的查询方向。
396

被折叠的 条评论
为什么被折叠?



