id     name    u_id  
1      张三    2
2      李四    3
3      王五    1字段u_id 就是上级的id  需要的到结果1      张三     李四
2      李四    王五
3      王五    张三 
请问语句要怎么写啊  

解决方案 »

  1.   


    select a.id,a.name,b.name from tb a ,tb b where a.uid=b.id 
      

  2.   


    declare @t table (id int,name varchar(4),u_id int)
    insert into @t
    select 1,'张三',2 union all
    select 2,'李四',3 union all
    select 3,'王五',1select a.id,a.name,b.name 
    from @t a LEFT JOIN @t b ON a.u_id=b.id
    /*
    id          name name
    ----------- ---- ----
    1           张三   李四
    2           李四   王五
    3           王五   张三
    */