您的位置:首页 > 数据库 > Oracle

Oracle 树操作

2020-07-18 05:27 507 查看
select…start with…connect by…prior

1.查找一个节点的所有直属子节点(节点本身+所有后代),有以下两种写法。

select id, typename, typecode, parentid, ordernum
from purchase_type
where status = 1
  start with id = '15'
  connect by parentid = prior id
  order siblings by ordernum

select id, typename, typecode, parentid, ordernum
from purchase_type
where status = 1
  start with id = '15'
  connect by prior id = parentid
  order siblings by ordernum

显示结果:

2.查找一个节点的所有直属父节点(节点本身+所有祖宗(父类的父类....))。

select id, typename, typecode, parentid, ordernum
from purchase_type
where status = 1
  start with id = '136'
  connect by id = prior parentid
  order siblings by ordernum

select id, typename, typecode, parentid, ordernum
from purchase_type
where status = 1
start with id = '136'
connect by prior parentid = id
order siblings by ordernum

显示结果:

转载于:https://www.cnblogs.com/chengx/p/5910227.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: