数据如下
ID HasChild Lev
1 0 1
2 0 1
3 2 2
4 2 2
5 3 3
6 0 1
7 0 1
8 0 1
9 0 1
10 0 1
11 10 2
12 0 1
13 12 2
14 0 1
15 14 2
这是一个树形结构 我的需求是每页显示五条,但是这五条必须包括根节点和子节点
分页效果如下
第一页:
1 0 1
2 0 1
3 2 2
4 2 2
5 3 3
6 0 1
7 0 1
8 0 1
第二页
9 0 1
10 0 1
11 10 2
12 0 1
13 12 2
14 0 1
15 14 2
sql? 分页查询
------解决方案--------------------
select 1 ID ,0 HasChild ,1 Lev
into #temp
union all select 2 ,0 ,1
union all select 3 ,2 ,2
union all select 4 ,2 ,2
union all select 5 ,3 ,3
union all select 6 ,0 ,1
union all select 7 ,0 ,1
union all select 8 ,0 ,1
union all select 9 ,0 ,1
union all select 10 ,0 ,1
union all select 11 ,10 ,2
union all select 12 ,0 ,1
union all select 13 ,12 ,2
union all select 14 ,0 ,1
union all select 15 ,14 ,2
declare @pagenum int=1;
declare @pagerownum int=5;
with t as
(
select id
from
(
select top (@pagerownum*@pagenum) *,ROW_NUMBER() over(order by id) rn
from #temp
where lev=1
) t
where rn>@pagerownum*(@pagenum-1)
)
select *
from #temp
where id in(select id from t)
or haschild in (select id from t)
------解决方案--------------------
这个根节点是lev为1的嘛,而子节点是否是这5个根节点的子节点呢
------解决方案--------------------
create table q10
(ID int,HasChild int,Lev int)
insert into q10
select 1, 0, 1 union all
select 2, 0, 1 union all
select 3, 2, 2 union all
select 4, 2, 2 union all
select 5, 3, 3 union all
select 6, 0, 1 union all
select 7, 0, 1 union all
select 8, 0, 1 union all
select 9, 0, 1 union all
select 10, 0, 1 union all
select 11, 10, 2 union all
select 12, 0, 1 union all
select 13, 12, 2 union all
select 14, 0, 1 union all
select 15, 14, 2
-- test1