最近做到找出所有的上級或者所有的下級的問題,需要用到遞迴
找出所有下屬員工id
with tab as(select *from sys_user where f_id='
'union all
select b.* from tab a,sys_user b where a.f_id=b.supervisorid)
select * from tab
其中 sys_user 是一張使用者表,f_id是主鍵,supervisorid是他領導者的id
同理可得,如果想找到所有的上級:
with tab as(select *from sys_user where f_id='
'union all
select b.* from tab a,sys_user b where a.supervisorid=b.f_id)
select * from tab
遞迴查詢SQL
lz需要的修改自己為對應的,csdn sqlserve大版主 鄒建 我轉貼並且完善一下 測試資料 create table tb id char 3 pid char 3 name nvarchar 10 insert tb select 001 null 山東省 union all select ...
SQL遞迴查詢
create table t bid int,p bid int insert into t select 1,null union all select 2,1 union all select 3,1 union all select 4,3 union all select 5,4 union...
SQL 遞迴查詢
create proc proc tree node int asbegin declare i int set i 0 set nocount on drop table res create table res node int,child int,weight int,op int inser...