select top 5 * from table order by id desc
现在显示前5条记录,能不能做成:有三个页面: 当然,按ID倒序排列的 三个页面,三个SQL语句第一个页面显示前15条---到第11条第二个页面显示第10条----到第6条第三个页显示:第5条--到第1条当数据库添加了一条记录,下面的显示就会变成:有三个页面: 当然,按ID倒序排列的第一个页面显示前16条---到第12条第二个页面显示第11条----到第7条第三个页显示:第6条--到第2条
这样的SQL能不能写,我三个页面,三个SQL语句
现在显示前5条记录,能不能做成:有三个页面: 当然,按ID倒序排列的 三个页面,三个SQL语句第一个页面显示前15条---到第11条第二个页面显示第10条----到第6条第三个页显示:第5条--到第1条当数据库添加了一条记录,下面的显示就会变成:有三个页面: 当然,按ID倒序排列的第一个页面显示前16条---到第12条第二个页面显示第11条----到第7条第三个页显示:第6条--到第2条
这样的SQL能不能写,我三个页面,三个SQL语句
create table t(id int identity(1,1),col_1 varchar(1), col_2 int)
insert t select
'a', 1 union all select
'b' , 2 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'c' , 3 union all select
'd', 5 --1
select top 5 * from t order by id desc--2
select top 5 * from t where id not in (select top 5 id from t order by id desc)order by id desc--3
select top 5 * from t where id not in (select top 10 id from t order by id desc)order by id descid col_1 col_2
----------- ----- -----------
17 d 5
16 c 3
15 c 3
14 c 3
13 c 3(5 行受影响)id col_1 col_2
----------- ----- -----------
12 c 3
11 c 3
10 c 3
9 c 3
8 c 3(5 行受影响)id col_1 col_2
----------- ----- -----------
7 c 3
6 c 3
5 c 3
4 c 3
3 c 3(5 行受影响)
drop table t
select top 5 * from t where id not in (select top @t1 id from t order by id desc)order by id desc@t1 为变量应该等于:总记录数-(当前所在页-1)*每页记录数up