加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 教程 > 正文

MSSQL中递归SQL查询语句实例详解-

发布时间:2021-12-02 11:09:20 所属栏目:教程 来源:互联网
导读:当前需要实现的功能:通过Number的值为67来获取当前的节点ID、父节点ID 递归实现SQL语句: 复制代码 代码如下: with znieyu as ( select c.Id,c.FatherId,0 as lv1 from ColumnTable c where c.Number=67 union all select c.Id,c.FatherId,lv1-1 from znieyu
当前需要实现的功能:通过Number的值为67来获取当前的节点ID、父节点ID
 
递归实现SQL语句:
 
复制代码 代码如下:
 
 
with znieyu as
(
select c.Id,c.FatherId,0 as lv1 from ColumnTable c where c.Number=67
union all
select c.Id,c.FatherId,lv1-1 from znieyu z
inner join ColumnTable c
on z.FatherId=c.Id
)
select znieyu.Id ,znieyu.FatherId,znieyu.lv1 as lv from znieyu
 
 
实现的效果:
 
满足条件67ID为6的递归-->6-->2(LV0)-->1(LV-1)-->0(LV-2)
满足条件67ID为8的递归-->8-->5(LV0)-->2(LV-1)-->1(LV-2)-->0(LV-3)
 很简单的一个SQL递归,记录下来...

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读