加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

oracle 递归 树形结构数据查询

发布时间:2020-12-12 18:45:40 所属栏目:百科 来源:网络整理
导读:connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with cond1 connect by cond2 where cond3; 例: select * from class start with parentid = 27362 Connect by prior id = parentid 简单说来是将一个树状结构存储在一张

connect by 是结构化查询中用到的,其基本语法是:
select ... from tablename start with cond1
connect by cond2
where cond3;

例:

select * from class
start with parentid = 27362
Connect by prior id = parentid

简单说来是将一个树状结构存储在一张表里,比如一个表中存在两个字段:
id,parentid那么通过表示每一条记录的parent是谁,就可以形成一个树状结构。
用上述语法的查询可以取得这棵树的所有记录。
其中COND1是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
COND2是连接条件,其中用PRIOR表示上一条记录,比如 CONNECT BY PRIOR ID=PRAENTID就是说上一条记录的ID是本条记录的PRAENTID,即本记录的父亲是上一条记录。
COND3是过滤条件,用于对返回的所有记录进行过滤。

--获取该id的所有父级
select * from Class
start with Id = '000500010003'
Connect by prior parentid= id

--获取该id的所有下级

select * from Class
start with Id= '000500010003'
Connect by prior id= parentid

工作实例:

select distinct * from usergroup b,groupinfo t where b.groupid=t.member and t.type = 2 start with t.groupid='oa_customerSrv' connect by prior t.member=t.groupid

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读