本帖最后由 xiaobadi 于 2011-10-20 21:32:39 编辑

解决方案 »

  1.   

    ;with f as
    (
    select * from tb where composite=@composite
    union all
    select a.* from tb a join f b on a.single=b.composite
    )
    select * from f
      

  2.   

    [code=SQL]
    create table #t(id int,childid int)
    insert into #t
    select 1 ,  2 union all
    select 2 ,  3 union all
    select 3 ,  4 union all
    select 5 ,  6;with t as
    (
    select flag = 1 , *  from #t where id = 1  --换成实际参数
    union all
    select flag = t.flag + 1 ,#t.* from #t  inner join t on  #t.id = t.childid
    )
    select top 1 childid from t order by flag descdrop table #t
    ----------------------------------------
    childid
    -----------
    4(1 行受影响)/code]
      

  3.   


    create table #t(id int,childid int)
    insert into #t
    select 1 , 2 union all
    select 2 , 3 union all
    select 3 , 4 union all
    select 5 , 6;with t as
    (
    select flag = 1 , * from #t where id = 1 --换成实际参数
    union all
    select flag = t.flag + 1 ,#t.* from #t inner join t on #t.id = t.childid
    )
    select top 1 childid from t order by flag descdrop table #t
    ----------------------------------------
    childid
    -----------
    4(1 行受影响)
      

  4.   

    楼上的两位大哥,;with f as是什么意思,我用的是mysql,支持你们的写法吗
      

  5.   

    参考下贴中的方法。http://blog.csdn.net/acmain_chm/article/details/4142971
    MySQL中进行树状所有子节点的查询