有这样一个表,设计如下id typename parid ordernum
编号 类别名称 父类ID 排序
有数据;1 网站制作 0 12 电脑维护 0 23 软件设计 0 34 前台维护 1 15 后台设计 1 26 系统维护 2 17 优化软件 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
请问该语句要怎么写
编号 类别名称 父类ID 排序
有数据;1 网站制作 0 12 电脑维护 0 23 软件设计 0 34 前台维护 1 15 后台设计 1 26 系统维护 2 17 优化软件 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
请问该语句要怎么写
id typename parid ordernum
编号 类别名称 父类ID 排序
---------------------
from tb
order by
case when parid!=0 then ordernum else parid end,
parid
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 t3id typename parid ordernum
1 网站制作 0 1
4 前台维护 1 1
5 后台设计 1 2
2 电脑维护 0 2
6 系统维护 2 1
3 软件设计 0 3
7 优化软件 3 1
union all select * from t_name where parid!=0 order by parid,ordernum
(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*/
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, 1gowith 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,ordernumdrop 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
*/
from tb
order by
case when parid!=0 then ordernum else parid end,
parid
from tb
order by
case when parid!=0 then (parid+ordernum) else (ordernum+parid) end
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
*/