关于sql排序问题

hflkl1314 2010-08-14 04:53:04
有这样一个表,设计如下

id typename parid ordernum


编号 类别名称 父类ID 排序


有数据;

1 网站制作 0 1

2 电脑维护 0 2

3 软件设计 0 3

4 前台维护 1 1

5 后台设计 1 2

6 系统维护 2 1

7 优化软件 3 1


我想要查询的到如下结果

id typename parid ordernum
1 网站制作 0 1
4 前台维护 1 1
5 后台设计 1 2
2 电脑维护 0 2
6 系统维护 2 1
3 软件设计 0 3
7 优化软件 3 1


请问该语句要怎么写

...全文
136 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQLCenter 2010-08-14
  • 打赏
  • 举报
回复
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(id int, name varchar(8), pid int)
insert into #
select 1, '网站制作', 0 union all
select 2, '电脑维护', 0 union all
select 3, '软件设计', 0 union all
select 4, '前台维护', 1 union all
select 5, '后台设计', 1 union all
select 6, '系统维护', 2 union all
select 7, '优化软件', 3

;with Sort as
(
select *, Sort = convert(varbinary(8000), convert(binary(2), id)) from # where pid = 0
union all
select a.*, b.Sort + convert(binary(2), a.id) from # a join Sort b on a.pid = b.id
)
select * from Sort order by Sort
/*
id name pid Sort
-------- -------- -------- ----------
1 网站制作 0 0x0001
4 前台维护 1 0x00010004
5 后台设计 1 0x00010005
2 电脑维护 0 0x0002
6 系统维护 2 0x00020006
3 软件设计 0 0x0003
7 优化软件 3 0x00030007
*/
飃颻 2010-08-14
  • 打赏
  • 举报
回复
select *
from tb
order by
case when parid!=0 then (parid+ordernum) else (ordernum+parid) end
ChinaITOldMan 2010-08-14
  • 打赏
  • 举报
回复
select *
from tb
order by
case when parid!=0 then ordernum else parid end,
parid
sheshuiping 2010-08-14
  • 打赏
  • 举报
回复
楼上的思路正确,不过cte 中的排序应使用ordernum栏位(不然上面的语句对物理存储顺序有依赖),修正后的语句如下:

create table #t (id int,typename varchar(20), parid int, ordernum int)
insert into #t
select 2,'电脑维护', 0, 2 union all
select 1,'网站制作', 0, 1 union all
select 3,'软件设计', 0, 3 union all
select 4,'前台维护', 1, 1 union all
select 5,'后台设计', 1, 2 union all
select 6,'系统维护', 2, 1 union all
select 7,'优化软件', 3, 1

go

with result as
(select * ,rn=ROW_NUMBER()over(order by ordernum) from #t where parid=0)
select id ,typename ,parid ,ordernum
from (
select * from result
union all
select k.*,c.rn from result c join #t k on c.ordernum=k.parid)k
order by rn,parid,ordernum

drop table #t

/*
(7 行受影响)
id typename parid ordernum
----------- -------------------- ----------- -----------
1 网站制作 0 1
4 前台维护 1 1
5 后台设计 1 2
2 电脑维护 0 2
6 系统维护 2 1
3 软件设计 0 3
7 优化软件 3 1
*/


feixianxxx 2010-08-14
  • 打赏
  • 举报
回复
create table kos
(id int, typename varchar(10), parid int,ordernum int)
insert kos select
1, '网站制作', 0, 1 union all select
4, '前台维护', 1, 1 union all select
5, '后台设计', 1, 2 union all select
2, '电脑维护', 0, 2 union all select
6, '系统维护', 2, 1 union all select
3, '软件设计', 0, 3 union all select
7, '优化软件', 3, 1
go
with cte as
(select * ,rn=ROW_NUMBER()over(order by getdate()) from kos where parid=0)
select id ,typename ,parid ,ordernum
from (
select * from cte
union all
select k.*,c.rn from cte c join kos k on c.ordernum=k.parid)k
order by rn,parid,ordernum
/*
id typename parid ordernum
----------- ---------- ----------- -----------
1 网站制作 0 1
4 前台维护 1 1
5 后台设计 1 2
2 电脑维护 0 2
6 系统维护 2 1
3 软件设计 0 3
7 优化软件 3 1*/
飃颻 2010-08-14
  • 打赏
  • 举报
回复
select * from t_name where parid==0
union all select * from t_name where parid!=0 order by parid,ordernum
hflkl1314 2010-08-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jaydom 的回复:]
SQL code

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int ,typename varchar(8),parid int,ordernum int)
insert into tb
select 1, '网站制作', 0, 1 union all
select 2, '电脑维护',……
[/Quote]

有简单一点大语句吗
hflkl1314 2010-08-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 josy 的回复:]
如果只有两级


SQL code
select *
from tb
order by
case when parid!=0 then ordernum else parid end,
parid
[/Quote]


这个测试无效
jaydom 2010-08-14
  • 打赏
  • 举报
回复

if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (id int ,typename varchar(8),parid int,ordernum int)
insert into tb
select 1, '网站制作', 0, 1 union all
select 2, '电脑维护', 0, 2 union all
select 3, '软件设计', 0, 3 union all
select 4, '前台维护', 1, 1 union all
select 5, '后台设计', 1, 2 union all
select 6, '系统维护', 2, 1 union all
select 7, '优化软件', 3, 1

;with t as
(
select * from tb where id=1
union all
select a.* from tb a join t on t.id=a.parid
),t1
as
(
select * from tb where id=2
union all
select a.* from tb a join t1 on t1.id=a.parid
),t2
as
(
select * from tb where id=3
union all
select a.* from tb a join t2 on t2.id=a.parid
), t3
as
(
select * from t
union all
select * from t1
union all
select * from t2
)
select * from t3

id typename parid ordernum
1 网站制作 0 1
4 前台维护 1 1
5 后台设计 1 2
2 电脑维护 0 2
6 系统维护 2 1
3 软件设计 0 3
7 优化软件 3 1
百年树人 2010-08-14
  • 打赏
  • 举报
回复
如果只有两级

select *
from tb
order by
case when parid!=0 then ordernum else parid end,
parid
hflkl1314 2010-08-14
  • 打赏
  • 举报
回复
写清楚了啊

---------------------
id typename parid ordernum


编号 类别名称 父类ID 排序
---------------------
飃颻 2010-08-14
  • 打赏
  • 举报
回复
字段ordernum是排序的还是parid,說不清楚。
hflkl1314 2010-08-14
  • 打赏
  • 举报
回复
排序规则是这样的

一级类别
一级类别下的所有二级类别

一级类别
一级类别下的所有二级类别

一级类别
一级类别下的所有二级类别
s_111111 2010-08-14
  • 打赏
  • 举报
回复
什麼規則。詳細說清楚

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧