Oracle数据库之层次化查询

--基本查询
select id,c_mmcode,c_mmroomname,c_parentId from s_mmroom;
--层次化查询
select id,c_mmcode,c_mmroomname,c_parentId from s_mmroom
start with c_parentId='0' connect by prior id = c_parentId;
--使用level节点
select level,id,c_mmcode,c_mmroomname,c_parentId from s_mmroom
start with c_parentId='0' connect by prior id = c_parentId;
--查询层次数
select count(distinct level) from s_mmroom
start with c_parentId='0' connect by prior id = c_parentId;
---查询结果的层次化
select level,LPAD(' ',2*LEVEL-1)||' '||c_mmcode||' '||c_mmroomname,c_parentId from s_mmroom
start with c_parentId='0' connect by prior id = c_parentId;
--从非根节点开始遍历
select level,LPAD(' ',2*LEVEL-1)||' '||c_mmcode||' '||c_mmroomname,c_parentId from s_mmroom
start with c_parentId='00000000000000000000000000000000' connect by prior id = c_parentId;
--在START WITH中使用子查询  下面这个查询使用子查询来选择指定节点的。然后传给START WITH子句。
select level,lpad(' ',2*level-1)||' '||c_mmroomname from s_mmroom
start with c_parentId=(select id from s_mmroom where c_mmroomname like '%中国船级社%')
connect by prior id =c_parentId;
--从下向上遍历树  不一定非要按照从父节点到子节点的顺序从上至下遍历树;也可以从某个子节点开始,从下而上遍历。实现的方法是交换父节点和子节点在CONNECT BY PRIOR子句中的顺序
select level,lpad(' ',2*level-1)||' '||c_mmroomname from s_mmroom
start with c_parentId='00000000000000000000000000000000' connect by prior id = c_parentId;
--从层次化查询中删除节点和分支   略 表名后面加where

--在层次化查询中加入其它条件      略 表名后面加where

猜你喜欢

转载自blog.csdn.net/weixin_40931184/article/details/80671880
今日推荐