表数据

select id,dept_nam,pid from dept

2020-05-28T06:34:56.png


查询树状结构某节点(75)的下级所有节点

with 
    dept_parent(id,dept_nam,pid) 
as
( 
  select 
    id,dept_nam,pid 
  from 
    dept 
  where 
    id = 75
  
  union all
  
  select 
    a.id,a.dept_nam,a.pid 
  from 
    dept a 
  inner join 
    dept_parent b
  on 
    a.pid=b.id  
)    
select * from dept_parent;

2020-05-28T06:37:38.png


查询树状结构某节点(77)的上级所有节点

with 
    dept_parent_s(id,dept_nam,pid) 
as
( 
  select 
    id,dept_nam,pid 
  from 
    dept 
  where 
    id = 77
  
  union all
  
  select 
    a.id,a.dept_nam,a.pid 
  from 
    dept a 
  inner join 
    dept_parent_s b
  on 
    a.id=b.pid
)    
select * from dept_parent_s

2020-05-28T06:39:13.png


版权声明:本文为原创文章,版权归 吾爱博客 所有,转载请联系博主获得授权!
本文地址:https://www.52bd.net/database/214.html

发表评论

正在加载 Emoji
×