前端之家收集整理的这篇文章主要介绍了
ORACLE 树形遍历父节点子节点,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、准备演示数据
创建表结构:
@H_404_13@-- Create table
createtable Z_ORG( cid NUMBER,cname VARCHAR2(32),parent_id NUMBER,create_time DATE,org_level NUMBER)
tablespace POWERDESK
pctfree10
initrans1
maxtrans255;
-- Add comments to the table
commentontable Z_ORG is'机构组织简化表';
-- Add comments to the columns
commentoncolumn Z_ORG.cid is'主键ID';
commentoncolumn Z_ORG.cname is'组织名称';
commentoncolumn Z_ORG.parent_id is'上级组织ID';
commentoncolumn Z_ORG.create_time is'创建时间';
commentoncolumn Z_ORG.org_level is'组织级别';
录入数据:
@H_404_13@ insertinto z_org(cid,cname,parent_id,create_time,org_level) select1,'地球',0,sysdate,1from dual;
insertinto z_org(cid,org_level) select2,'中国',1,2from dual;
insertinto z_org(cid,org_level) select3,'上海直辖市',2,3from dual;
insertinto z_org(cid,org_level) select4,'江苏省',org_level) select5,'南京市',4,4from dual;
insertinto z_org(cid,org_level) select6,'苏州市',org_level) select7,'无锡市',org_level) select8,'虹口区',3,org_level) select9,'浙江省',org_level) select10,'杭州市',9,4from dual;
insertinto z_org(cid,org_level) select11,'宁波市',org_level) select12,'美国',org_level) select13,'加利福尼亚州',12,org_level) select14,'旧金山市',13,org_level) select15,'撒门市',4from dual;
commit;
2、遍历根节点
1. selectcode1fromtablename
2. startwithcode2
3. connectbycode3
4. wherecond3
code2是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
code3是连接条件,其中用prior表示上一条记录,比如connect by prior id=parentid就是说上一条记录的id是本条记录的parent,即本记录的父亲是上一条记录。
code4是过滤条件,用于对返回的所有记录进行过滤。
prior和start with关键字是可选项:
prior运算符必须放置在连接关系的两列中某一个的前面。对于节点间的父子关系,prior运算符在一侧表示父节点,在另一侧表示子节点,从而确定查找树结构是的顺序是自顶向下还是自底向上。在连接关系中,除了可以使用列名外,还允许使用列表达式。start with子句为可选项,用来标识哪个节点作为查找树型结构的根节点。若该子句被省略,则表示所有满足查询条件的行作为根节点。
遍历表,求得某一个节点的所有上级节点记录,比如求得撒门市的上级组织,应该获得加利福尼亚州,美国,地球这些记录,如下所示:
@H_404_13@sql> select t.cid,t.cname,t.parent_id,level
2 from z_org t
3 startwith t.cname='撒门市'
4 connectbynocycleprior t.parent_id=t.cid
5 orderbylevelasc
6 ;
CID CNAME PARENT_ID LEVEL
---------- -------------------------------- ---------- ----------
15撒门市 13 1
13加利福尼亚州 12 2
12美国 1 3
1地球 0 4
sql>
遍历求得根路径字符串:
@H_404_13@sql> select cname_child,wm_concat(t2.cname)
2 from(
3 select'苏州市' cname_child,t.cid,level
4 from z_org t
5 startwith t.cname in('苏州市')
6 connectbynocycleprior t.parent_id=t.cid
7 )t2 groupby cname_child ;
CNAME_CHILD WM_CONCAT(T2.CNAME)
----------- --------------------------------------------------------------------------------
苏州市 苏州市,地球,中国,江苏省
sql>
3、遍历子节点
5. selectcode1fromtablename
6. startwithcode2
7. connectbycode3
8. wherecond3
code2是根结点的限定语句,当然可以放宽限定条件,以取得多个根结点,实际就是多棵树。
code3是连接条件,其中用prior表示上一条记录,比如connect by prior id=parentid就是说上一条记录的id是本条记录的parent,即本记录的父亲是上一条记录。
code4是过滤条件,用于对返回的所有记录进行过滤。
prior和start with关键字是可选项:
prior运算符必须放置在连接关系的两列中某一个的前面。对于节点间的父子关系,prior运算符在一侧表示父节点,在另一侧表示子节点,从而确定查找树结构是的顺序是自顶向下还是自底向上。在连接关系中,除了可以使用列名外,还允许使用列表达式。
start with子句为可选项,用来标识哪个节点作为查找树型结构的根节点。若该子句被省略,则表示所有满足查询条件的行作为根节点。
如下所示,prior偏向父节点,就往下查询,查询节点下面所有的子节点记录:
@H_404_13@select t1.parent_id,t1.cid,casewhenlevel=1then'*'||t1.cname whenlevel=2then'*-----'||t1.cname whenlevel=3then'*-----*-----'||t1.cname else t1.cname end,level
from z_org t1
startwith t1.parent_id = 1
connectby t1.cid=prior t1.parent_id
;
如下所示E:\u\oracle\problem\pic\52.png:
如下所示,prior偏向子节点,就往下查询,查询节点下面所有的子节点记录:
@H_404_13@select t1.parent_id,level
from z_org t1
startwith t1.parent_id = 1
connectbyprior t1.cid=t1.parent_id
;
执行结果如下所示E:\u\oracle\problem\pic\51.png:
4、扩展研究
---从parentid到cid开始递归,并以为主展示这条记录
select t1.parentid,level
from test_category t1
start with t1.parentid = 1
connect by prior t1.parentid = t1.cid
;
---从parentid到cid开始递归,并以parentid为主展示这条记录
select t1.parentid,level
from test_category t1
start with t1.cid = 1
connect by prior t1.parentid = t1.cid
;
---从parentid到cid开始递归,并以cid为主展示这条记录
select t1.cid,t1.parentid,level
from test_category t1
start with t1.parentid = 1
connect by t1.parentid = prior t1.cid
;
---从parentid到cid开始递归,并以cid为主展示这条记录select t1.cid,levelfrom test_category t1start with t1.cid = 1connect by t1.parentid = prior t1.cid;