id name parentid
2000 菜系 null
2001 省 null
2002 车位 null
2003 人均消费 null
2004 市 2001
2005 县 2004
现在要执行一sql,得到如下结果
id name parentid
2000 菜系 null
2001 省 null
2004 市 2001
2005 县 2004
2002 车位 null
2003 人均消费 null
2000 菜系 null
2001 省 null
2002 车位 null
2003 人均消费 null
2004 市 2001
2005 县 2004
现在要执行一sql,得到如下结果
id name parentid
2000 菜系 null
2001 省 null
2004 市 2001
2005 县 2004
2002 车位 null
2003 人均消费 null
*
from tb order by case when name='菜系' then 0
when name='省' then 1 else 2 end,case when parentid is nul then 9999
else parentid end
--------------------------------------------------------------------------------------------------------------------------------
create table t (id int,name varchar(10),parentid int)
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
create function f_str(@id int)
returns varchar(1000)
as
begin
declare @ret varchar(1000),@pid int
select @pid=parentid,@ret=right('0000'+rtrim(id),5) from t where id=@id
while @pid is not null
begin
set @id=@pid
select @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret from t where id=@id
end
return @ret
end
go
select * from t order by dbo.f_str(id)
go/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop function f_str
drop table t
*
from tb
order by case when name='菜系' then 0
when name='省' then 1 else 2 end,
case when parentid is nul then 99999
else parentid end
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
create function f_str(@id int)
returns varchar(1000)
as
begin
declare @ret varchar(1000),@pid int
select @pid=parentid,@ret=right('0000'+rtrim(id),5) from t where id=@id
while @pid is not null
begin
set @id=@pid
select @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret from t where id=@id
end
return @ret
end
go
select * from t order by dbo.f_str(id)
go/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop function f_str
drop table t
insert into @t select 2000,'菜系 ',null
insert into @t select 2001,'省 ',null
insert into @t select 2002,'车位 ',null
insert into @t select 2003,'人均消费',null
insert into @t select 2004,'市 ',2001
insert into @t select 2005,'县 ',2004select
*
from @t
order by case when name='菜系' then 0
when name='省' then 1 else 2 end,
case when parentid is null then 99999
else parentid end结果:
id name parentid
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
create table t (id int,name varchar(10),parentid int)
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
select
*
from t order by case when name='菜系' then 0
when name='省' then 1 else 2 end,case when parentid is null then 9999
else parentid end
/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop table t