如下题,我在表里再加了一个排序字段orderno
 要求,同一级的分类按orderno desc(或asc)排序,应怎么改呀.
试了很久,排序不正确
create table test
(
ID int,
classname Nvarchar(10),
parentID int,
orderno int
)
insert into test
select 1,N'中国',0 union all
select 2,N'上海',1 union all
select 3,N'江西',1 union all
select 4,N'浙江',1 union all
select 5,N'江苏',1 union all
select 6,N'南昌',3 union all
select 7,N'杭州市',4 union all
select 8,N'九江',3 union all
select 9,N'温州',4 union all
select 10,N'英国',0 union all
select 11,N'法国',0
go
create function f_root(@a int)
returns varchar(10)
as
begin
declare @pid int
declare @return varchar(1000)
select @pid=@a
select @return=''
while @pid>=1
begin
select @return=rtrim(@pid)+','+@return
select @pid=parentid from test where id=@a
select @a=@pid
end
return @return
end
go
select id,classname
from test
order by dbo.f_root(id)
GO
Drop  function f_root
Drop table test
--Result
/*
idclassname
1中国
2上海
3江西
6南昌
8九江
4浙江
7杭州市
9温州
5江苏
10英国
11法国
*/  

解决方案 »

  1.   

    paoluo(一天到晚游泳的鱼)大哥请进........
      

  2.   

    paoluo(一天到晚游泳的鱼)大哥请进........
      

  3.   

    select dbo.f_root(id) from test
    order by substring(dbo.f_root(id),3,1),substring(dbo.f_root(id),5,1)
      

  4.   

    这样可以按照国家排:
    select dbo.f_root(id) from test
    order by substring(dbo.f_root(id),2,1),substring(dbo.f_root(id),3,1),substring(dbo.f_root(id),5,1)
      

  5.   

    create table test
    (
    ID int,
    classname Nvarchar(20),
    parentID int
    )
    insert into test
    select 1,N'中国',0 union all
    select 2,N'上海',1 union all
    select 3,N'江西',1 union all
    select 4,N'浙江',1 union all
    select 5,N'江苏',1 union all
    select 6,N'南昌',3 union all
    select 7,N'杭州市',4 union all
    select 8,N'九江',3 union all
    select 9,N'温州',4 union all
    select 10,N'英国',0 union all
    select 11,N'法国',0
    gocreate function f_root(@a int)
    returns varchar(10)
    as
    begin
    declare @pid int
    declare @return varchar(1000)
    select @pid=@a
    select @return=''
    while @pid>=1
    begin
    select @return=rtrim(@pid)+','+@return
    select @pid=parentid from test where id=@a
    select @a=@pid
    end
    return @return
    end
    go
    select id,classname
    from test
    order by substring(dbo.f_root(id),2,1),substring(dbo.f_root(id),3,1),substring(dbo.f_root(id),5,1)GO
    Drop  function f_root
    Drop table test/*
    1 中国
    2 上海
    3 江西
    6 南昌
    8 九江
    4 浙江
    7 杭州市
    9 温州
    5 江苏
    10 英国
    11 法国
    */
      

  6.   

    如下题,我在表里再加了一个排序字段orderno
     要求,同一级的分类按orderno desc(或asc)排序,应怎么改呀.
    试了很久,排序不正确不加orderno 排序字段,
    下面代码:
    create table test
    (
    ID int,
    classname Nvarchar(10),
    parentID int,
    orderno int
    )
    insert into test
    select 1,N'中国',0 union all
    select 2,N'上海',1 union all
    select 3,N'江西',1 union all
    select 4,N'浙江',1 union all
    select 5,N'江苏',1 union all
    select 6,N'南昌',3 union all
    select 7,N'杭州市',4 union all
    select 8,N'九江',3 union all
    select 9,N'温州',4 union all
    select 10,N'英国',0 union all
    select 11,N'法国',0
    go
    create function f_root(@a int)
    returns varchar(10)
    as
    begin
    declare @pid int
    declare @return varchar(1000)
    select @pid=@a
    select @return=''
    while @pid>=1
    begin
    select @return=rtrim(@pid)+','+@return
    select @pid=parentid from test where id=@a
    select @a=@pid
    end
    return @return
    end
    go
    select id,classname
    from test
    order by dbo.f_root(id)
    GO
    Drop  function f_root
    Drop table test
    --Result
    /*
    idclassname
    1中国
    2上海
    3江西
    6南昌
    8九江
    4浙江
    7杭州市
    9温州
    5江苏
    10英国
    11法国
    */
    可以实现现在问题是;
    我多加一定字段orderno排序:
    试怎么改
      

  7.   

    你orderno的值都没有,让人家怎么做-_-
      

  8.   

    create table test
    (
    ID int,
    classname Nvarchar(10),
    parentID int,
    orderno int
    )
    insert into test
    select 1,N'中国',0,1 union all
    select 2,N'上海',1,1 union all
    select 3,N'江西',1,4 union all
    select 4,N'浙江',1,6 union all
    select 5,N'江苏',1,5 union all
    select 6,N'南昌',3,7 union all
    select 7,N'杭州市',4,2 union all
    select 8,N'九江',3,8 union all
    select 9,N'温州',4 ,9 union all
    select 10,N'英国',0,2 union all
    select 11,N'法国',0,3
    go
    create function f_root(@a int)
    returns varchar(10)
    as
    begin
    declare @pid int
    declare @return varchar(1000)
    select @pid=@a
    select @return=''
    while @pid>=1
    begin
    select @return=rtrim(@pid)+','+@return
    select @pid=parentid from test where id=@a
    select @a=@pid
    end
    return @return
    end
    go
    select id,classname
    from test
    order by dbo.f_root(id)
    GO
    Drop  function f_root
    Drop table test
    --Result
    /*
    idclassname
    1中国
    2上海
    3江西
    6南昌
    8九江
    4浙江
    7杭州市
    9温州
    5江苏
    10英国
    11法国
    */
      

  9.   

    create table test
    (
    ID int,
    classname Nvarchar(10),
    parentID int,
    orderno int
    )
    insert into test
    select 1,N'中国',0,2 union all
    select 2,N'上海',1,4 union all
    select 3,N'江西',1,3 union all
    select 4,N'浙江',1,5 union all
    select 5,N'南昌',3,2 union all
    select 6,N'杭州市',4,2 union all
    select 7,N'九江',3,1 union all
    select 8,N'温州',4 ,1 union all
    select 9,N'英国',0,1 union all
    select 10,N'法国',0,3
    go
    create function f_root(@a int)
    returns varchar(10)
    as
    begin
    declare @pid int
    declare @return varchar(1000)
    select @pid=@a
    select @return=''
    while @pid>=1
    begin
    select @return=rtrim(orderno)+' '+rtrim(@pid)+' '+@return,@pid=parentid from test where id=@a
    select @a=@pid
    end
    return @return
    end
    go
    select id,classname
    from test
    order by dbo.f_root(id)
    GO
    Drop  function f_root
    Drop table test/*
    id          classname  
    ----------- ---------- 
    9           英国
    1           中国
    3           江西
    7           九江
    5           南昌
    2           上海
    4           浙江
    8           温州
    6           杭州市
    10          法国*/