偶详观各数据库SQL,得出是数据库就有取前面N条记录的SQL语法,如什么select top n*****之类的语法,而数据分页的关键问题是取后N条记录的语法偶深思良久,最后小悟,故出此言,还忘前辈们多多指点
取记录集后N条记录的大法:
假设:
1.有一sql语句将产生1000条记录 (select 唯一ID,其他字段 from 表集 where 条件 order by 排序)
2.每页显示20条记录
3.当前显示第5页
实现如下:
select * from
(
select * from (select top 20*4 唯一ID,其他字段 from 表集 where 条件 order by 排序) as a
union all
select * from (select top 20*5 唯一ID,其他字段 from 表集 where 条件 order by 排序) as b
)
a
group by 唯一ID,其他字段 having count(唯一ID)=1 order by 排序