用sql 查询时select top 10 * from Orders order by orderid desc操作时,我们先对查询结果进行ordersby排序 然后才取出其最前边10个
使用sql查询分页时的几点思路(举例均从查询第100到120之间20条记录)
1.取前120条记录orderby主键,然后再从结果中取倒序排列的20条记录
例:
select top 20* from (select Top 120* from orders order by orderid )a order by orderid desc
select top 20* from (select Top 120* from orders order by orderid )a order by orderid desc
2.取前一百个记录的最大orderid,然后查找第100开始的20个记录
例:
select top 20* from orders where orderid >(select max(orderid)from (select top 100* from orders order by orderid)b )order by orderid
3.查询操作和第一种差不多
例:
select top 20 * from orders where orderid in (select top 120 orderid from orders order by orderid) order by orderid desc