62,041
社区成员
发帖
与我相关
我的任务
分享
Id LId
1 0
2 1
3 2
4 3
5 0
6 5
-- 输入的参数是Id,怎么返回他的根父级啊
--比如:输入4,返回1,输入3,返回1 输入6,返回5,输入5,返回5
create table #tb(id int,LId int)
insert into #tb
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,3 union all
select 5,0 union all
select 6,5
declare @ID int
set @ID=4
;with cte as(
select * from #tb where ID=@ID union all
select t.* from cte c inner join #tb t on t.id=c.lid
)
select top 1 id from cte order by id
/*
1
*/
create table #tb(id int,LId int)
insert into #tb
select 1,0 union all
select 2,1 union all
select 3,2 union all
select 4,3 union all
select 5,0 union all
select 6,5
select * from #tb
declare @ID int
set @ID=5
;with cte as(
select * from #tb where ID=@ID union all
select t.* from cte c inner join #tb t on t.id=c.lid
)
select top 1 id from cte order by id
Hashtable ht = new Hashtable();
//ht.Add("子节点ID","父节点ID");
ht.Add(1,0);
ht.Add(2,1);
ht.Add(3,1);
ht.Add(4,1);
Response.Write(ht[2].ToString());
//显示的就是ID为2的父节点ID 为1
//表结构就按照那个弄吧