select * from tablename order by 父节点ID号,节点ID号

解决方案 »

  1.   

    谢谢!
    不过节点ID号不一定有规则,怎么办呢?
    比如表中的数据
    序号     节点ID号   父节点ID号   节点名称
    1          A1        B2       XXXXX       
    2          B2        -1       XXXXX                       
    3          U13       U1       XXXXX      
    4          U4        U13      XXXXX      
    5          U5        U2       XXXXX
      

  2.   

    应该没有事的,
    我的意思是先按父节点ID号排序,如果父节点ID号一样就按节点ID号排序
      

  3.   

    hsj20041004(光芒)没理解楼主的意思关注.
      

  4.   


    你好!
    这样的排序还是不行
    (注意的是:节点不是数字类型)
    比如:
    节点ID号  父节点ID号  节点顺序
    10   4    1
    2   10    2
    3   0    3
    4   0    1
    5   2    1
    6   3    1
    select * from tablename order by 父节点ID号,节点ID号排序后:
    节点ID号  父节点ID号 节点顺序
    3   0    3
    4   0    1
    2   10    2
    5   2    1
    6   3    1
    10   4    1
    希望结果:
    节点ID号  父节点ID号 节点顺序
    4   0    1   
    3   0    3  <-父节点ID号0表示为第一级目录,根据节点顺序,处于节点4的后面(因为节点4的顺序为1,在节点3的前面)
    10   4    1  <-在节点4的后面
    2   10    2  <-在节点10的后面
    5   2    1  <-在节点2的后面
    6   3    1  <-这一项位置只要在 节点ID为3的后面就可以了
      

  5.   

    To:softj(天地客人<最近很迷茫>) 
    你好
    怎么实现呢?谢谢!!
      

  6.   

    create table A
    (
      [序号] int,
      [节点ID号] varchar(10),
      [父节点ID号] varchar(10),
      [节点名称] varchar(10)
    )
    insert A
    select 1,'U1','U2','XXXXX' union
    select 2,'U2','-1','XXXXX' union
    select 3,'U3','U1','XXXXX' union
    select 4,'U4','U3','XXXXX' union
    select 5,'U5','U2','XXXXX' 
    goif exists(select 1 from sysobjects where id=object_id('f_level') and xtype='FN')
    drop function f_level
    go
    create function f_level(@ID varchar(10))
    returns int
    as
    begin
          declare @i int
          set @i=1
          while (select [父节点ID号] from A where [节点ID号]=@ID)<>'-1'
          begin
                select @ID=[父节点ID号] from A where [节点ID号]=@ID
                set @i=@i+1
          end      return @i
    end
    go--查询
    select * from A order by dbo.f_level([节点ID号])--删除测试环境
    drop function f_level
    drop table A--结果
    /*
    序号          节点ID号      父节点ID号     节点名称       
    ----------- ---------- ---------- ---------- 
    2           U2         -1         XXXXX
    1           U1         U2         XXXXX
    5           U5         U2         XXXXX
    3           U3         U1         XXXXX
    4           U4         U3         XXXXX(5 row(s) affected)
    */