通过Sql语句优化Web分页功能
web编程常常遇到分页的问题,如何仅读取当前页的数据是一个问题。mysql有一个“From n to m”的语句可以很方便的获得,Mssql就有些困难,下面是我构造出来的类似功能。
PageSize:每页记录数;
StartIndex:当前几页,从0开始;
tmpOrderby:排序
select * from
(select top PageSize * from
(select top PageSize*(StartIndex+1) * from Table
where Condition order by tmpOrderby) tmpTable1
order by tmpOrderby desc) tmpTable2
order by tmpOrderby
(select top PageSize * from
(select top PageSize*(StartIndex+1) * from Table
where Condition order by tmpOrderby) tmpTable1
order by tmpOrderby desc) tmpTable2
order by tmpOrderby