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


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;


posted @ 2017-09-07 15:31  程序员老油条  阅读(79)  评论(0)    收藏  举报