oracle 递归查询(树查询) connect by prior

2017/09 07 07:09


Oracle的 connect by 语句,可用来做有父子关系的表向上或向下查询:


// 1、从id为1的开始查询所有子级
select * from org connect by prior id=pid start with id=1;

// 2、从id为1的开始查询所有上级

select * from org connect by prior pid=id start with id=1;

其中 prior 关键字,如果不加,则不做递归深层节点查询。


3、如果需要指定查询到哪一层级,可以指定 level ,如:

select * from org where
level = 2 connect by prior pid=id start with id=1;