如下题,我在表里再加了一个排序字段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法国
*/
解决方案 »
- !!!数据库能读不能写,怎么回事??!!
- 散分有图,回答正确再加40分,急在线等
- 恢复数据库后怎么总是 loading
- 这样子数据库可以恢复吗 ?(有mdf,ldf文件)
- 請問SQL中我要取一個浮點數的整數部分,可用哪個函數?謝謝
- 数据库管理与应用教程 制作一个带用户认证功能的教务管理系统 对于不同的用户赋予不同的管理权限 对于具有权限的用户给予增删改功能 对于能出现的错误进行预处理
- 询问最有效率的不重复数据插入方法
- 关于sql server的一个排序问题,关于分页相关
- 如何得到数据库表格数及名表格名称
- 关于vfp,Grid的一个问题?
- 关于SQL的子查询问题,高手请进
- 在某一服务器通过对象名字查找对应数据库名的存储过程。
order by substring(dbo.f_root(id),3,1),substring(dbo.f_root(id),5,1)
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)
(
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 法国
*/
要求,同一级的分类按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排序:
试怎么改
(
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法国
*/
(
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 法国*/