sql server查詢所有上級/下級

//上級
with test_student(stu_id, stu_name)
as
(
select stu_id, stu_name
from sys_student
where school_id = ‘1’ and del_flag = 0
union all
select a.parent_id, a.stu_id, a.stu_name
from sys_student a
inner join
test_student b
on a.stu_id=b.parent_id where a.del_flag = 0
)
select stu_id, stu_name from test_student order by stu_id
//下級
with test_student(stu_id, stu_name)
as
(
select stu_id, stu_name
from sys_student
where school_id = ‘1’ and deleted_flag = 0
union all
select a.parent_id, a.stu_id, a.stu_name
from sys_student a
inner join
test_student b
on a.parent_id=b.stu_id where a.deleted_flag = 0
)
select stu_id, stu_name from test_student order by stu_id

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章