自加入CTE递归 [英] self join CTE recursive
本文介绍了自加入CTE递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,frds
我有下表
StaffId等级ID
-----------------------
M1 M1
s1 M1
s2 M1
s3 M1
s7 s1
s10 s7
s8 s5
a k
结果
-----------
输入:M1
输出:
M1 M1
s1 M1
s2 M1
s3 M1
s7 s1
s10 s7
请回复....
Hi frds
I have the following table
StaffId Level Id
-----------------------
M1 M1
s1 M1
s2 M1
s3 M1
s7 s1
s10 s7
s8 s5
a k
result
-----------
input : M1
output:
M1 M1
s1 M1
s2 M1
s3 M1
s7 s1
s10 s7
please reply ....
推荐答案
with CTE_List as
(
select * from YourTable t where t.StaffId = @M1
union all
select * from CTE_List p, YourTable t where t.LevelId = p.StaffId
)
select * from CTE_List
@ M1是您的输入变量.
@M1 is your input variable.
这篇关于自加入CTE递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文