Oracle 语句递归查找父子关系语句

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 03:32   49   0


根据部门模糊搜索 ,后台会自动拼树


select t.*,level from b_Jt_Departments t start with dept_name like '%科%' and parent_id='302' connect by prior t.dept_id=t.parent_id

 --通过根节点遍历子节点
select t.*,level from family t start with parentid=1 connect by prior id=parentid; 

--通过子节点向根节点追溯
select t.*,level from family t start with id=5 connect by prior parentid=id;        

--查找直接子节点(下一层)
select t.*,level from family t where level = 2 start with parentid=1 connect by prior id=parentid;  

 --查找直接父节点(上一层)
select t.*,level from family t where level = 2 start with id=5 connect by prior parentid=id;       

--通过根节点遍历子节点
select level,t.* from ORG_DEPARTMENT_INFO t start with parent_id=-1 connect by prior dept_id=parent_id;  
--查找直接子节点(下一层)
select level,t.* from ORG_DEPARTMENT_INFO t where level = 2 start with parent_id=-1 connect by prior dept_id=parent_id;  
 --通过子节点向根节点追溯
select level,t.* from ORG_DEPARTMENT_INFO t start with dept_id=10000260 connect by prior parent_id=dept_id;       
--查找直接父节点(上一层)
select level,t.* from ORG_DEPARTMENT_INFO t where level = 2 start with dept_id=10000260 connect by prior parent_id=dept_id;     
 --查找直接父节点(上一层)   
select t.* from ORG_DEPARTMENT_INFO t where level = 2 start with dept_id=10000260 connect by prior parent_id=dept_id;       

--通过根节点向子节点追溯

select level, t.*from SM_ORGANIZATION t start with t.org_id='114e0e3c-dbd1-4c2e-9d10-d5fd1e243961' connect by prior org_id=parent_id; 
 --通过子节点向根节点追溯
select level, t.*  from SM_ORGANIZATION t start with org_id='9e67e1f4-f4b0-4669-b4e1-1dca132bb4d7' connect by prior parent_id=org_id;       


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP