Id LId
1 0
2 1
3 2
4 3
5 0
6 5-- 输入的参数是Id,怎么返回他的根父级啊
--比如:输入4,返回1,输入3,返回1 输入6,返回5,输入5,返回5

解决方案 »

  1.   

    类似tree的遍历,找到LID不是为0为止?
      

  2.   

    如果把LId改成ParentId是否更好理解一点呢
    4的父级是3,3的父级是2,2的父级是1,  4的根父级就是1,1没有父级,就是根父级
      

  3.   


    实现tree的做出来了,现在想根据一个Id找到根父级的Id,不知道该怎么写了
      

  4.   

    方法很多你可以用一个哈希表来存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
                //表结构就按照那个弄吧
      

  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 select * from #tbdeclare @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
      

  6.   


    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
    */