Oracle递归查询父子兄弟节点
1、查询某节点下所有后代节点(包括各级父节点)
1 // 查询id为101的所有后代节点,包含101在内的各级父节点
2 select t.* from SYS_ORG t start with id = '101' connect by parent_id = prior id
2、查询某节点下所有后代节点(不包含各级父节点)
1 select t.*
2 from SYS_ORG t
3 where not exists (select 1 from SYS_ORG s where s.parent_id = t.id)
4 start with id = '101'
5 connect by parent_id = prior id
3、查询某节点所有父节点(所有祖宗节点)
1 select t.*
2 from SYS_ORG t
3 start with id = '401000501'
4 connect by prior parent_id = id
4、查询某节点所有的兄弟节点(亲兄弟)
1 select * from SYS_ORG t
2 where exists (select * from SYS_ORG s where t.parent_id=s.parent_id and s.id='401000501')
5、查询某节点所有同级节点(族节点),假设不设置级别字段

1 with tmp as(
2 select t.*, level leaf
3 from SYS_ORG t
4 start with t.parent_id = '0'
5 connect by t.parent_id = prior t.id)
6 select *
7 from tmp
8 where leaf = (select leaf from tmp where id = '401000501');

这里使用两个技巧,一个是使用了level来标识每个节点在表中的级别,还有就是使用with语法模拟出了一张带有级别的临时表
6、查询某节点的父节点及兄弟节点(叔伯节点)

with tmp as(
select t.*, level lev
from SYS_ORG t
start with t.parent_id = '0'
connect by t.parent_id = prior t.id)
select b.*
from tmp b,(select *
from tmp
where id = '401000501' and lev = '2') a
where b.lev = '1' union all select *
from tmp
where parent_id = (select distinct x.id
from tmp x, --祖父
tmp y, --父亲
(select *
from tmp
where id = '401000501' and lev > '2') z --儿子
where y.id = z.parent_id and x.id = y.parent_id);

这里查询分成以下几步。
首先,将全表都使用临时表加上级别;
其次,根据级别来判断有几种类型,以上文中举的例子来说,有三种情况:
(1)当前节点为顶级节点,即查询出来的lev值为1,那么它没有上级节点,不予考虑。
(2)当前节点为2级节点,查询出来的lev值为2,那么就只要保证lev级别为1的就是其上级节点的兄弟节点。
(3)其它情况就是3以及以上级别,那么就要选查询出来其上级的上级节点(祖父),再来判断祖父的下级节点都是属于该节点的上级节点的兄弟节点。
最后,就是使用union将查询出来的结果进行结合起来,形成结果集。
Oracle递归查询父子兄弟节点的更多相关文章
- JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删改查),事件
JavaScript---Dom树详解,节点查找方式(直接(id,class,tag),间接(父子,兄弟)),节点操作(增删改查,赋值节点,替换节点,),节点属性操作(增删改查),节点文本的操作(增删 ...
- [javascript] jquery的父子兄弟节点查找
jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...
- 【转载】Oracle递归查询:使用prior实现树操作【本文出自叶德华博客】
本文标题:Oracle递归查询:使用prior实现树操作 本文链接:http://yedward.net/?id=41 本文版权归作者所有,欢迎转载,转载请以文字链接的形式注明文章出处. Oracle ...
- Oracle递归查询start with connect by prior
一.基本语法 connect by递归查询基本语法是: select 1 from 表格 start with ... connect by prior id = pId start with:表示以 ...
- ORACLE递归查询(适用于ID,PARENTID结构数据表)
Oracle 树操作(select…start with…connect by…prior) oracle树查询的最重要的就是select…start with…connect by…prior语法了 ...
- Oracle递归查询与常用分析函数
最近学习oracle的一些知识,发现自己sql还是很薄弱,需要继续学习,现在总结一下哈. (1)oracle递归查询 start with ... connect by prior ,至于是否向上查 ...
- JQuery的父、子、兄弟节点查找方法
jQuery.parent(expr) //找父元素 jQuery.parents(expr) //找到所有祖先元素,不限于父元素 jQuery.children ...
- 【2016-11-7】【坚持学习】【Day22】【Oracle 递归查询】
直接在oracle 递归查询语句 select * from groups start with id=:DeptId connect by prior superiorid =id 往下找 sele ...
- Oracle 11g RAC 第二节点root.sh执行失败后再次执行root.sh
Oracle 11g RAC 第二节点root.sh执行失败后再次执行root.sh前,要先清除之前的crs配置信息 # /u01/app/11.2.0/grid/crs/install/rootcr ...
随机推荐
- 读DataSnap源代码(四)
继续篇中的 function TCustomWebDispatcher.DispatchAction(Request: TWebRequest; Response: TWebResponse): Bo ...
- pyhanlp 共性分析与短语提取内容详解
pyhanlp 共性分析与短语提取内容详解 简介 HanLP中的词语提取是基于互信息与信息熵.想要计算互信息与信息熵有限要做的是 文本分词进行共性分析.在作者的原文中,有几个问题,为了便于说明,这 ...
- Mysql权限速查表以及权限详解
一.前言 很多文章中会说,数据库的权限按最小权限为原则,这句话本身没有错,但是却是一句空话.因为最小权限,这个东西太抽象,很多时候你并弄不清楚具体他需要哪 些权限. 现在很多mysql用着root账户 ...
- Facebook Login api
http://blog.kenyang.net/2012/01/androidfacebook-login-api.html http://blog.kenyang.net/2012/01/faceb ...
- ApplicationDomain
ApplicationDomain 类的用途是存储 ActionScript 3.0 定义表.SWF 文件中的所有代码被定义为存在于ApplicationDomain 中.在使用 Loader 类 A ...
- bzoj4865: [Ynoi2017]由乃运椰子
在线询问区间众数,传统的分块(记录块间众数和每个权值的出现次数)做法被卡空间(分块用的空间是O(块数*(块数+权值种类数))),因此考虑去掉出现次数较小的数,只用分块维护出现次数较大的数.设K为分界线 ...
- 结构方程软件Lisrel 8.7 和HLM5.5
这是我亲自使用过的软件,其中lisrel是破解版的,HLM是学生版的 下载地址:http://pan.baidu.com/s/1bnfCOrH
- P【1012】拼数
十分蒟蒻...(还是看别人的博才过的...) 题解 #include<cstdio>#include<cstring>#include<algorithm>#inc ...
- PAT 甲级 1011 World Cup Betting (20)(20 分)
1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...
- 服务网关zuul之二:过滤器--请求过滤执行过程(源码分析)
Zuul的核心是一系列的过滤器,这些过滤器可以完成以下功能: 身份认证与安全:识别每个资源的验证要求,并拒绝那些与要求不符的请求. 审查与监控:在边缘位置追踪有意义的数据和统计结果,从而带来精确的生成 ...