码迷,mamicode.com
首页 > 其他好文 > 详细

ROW_NUMBER分页的错误使用

时间:2015-02-10 12:57:58      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

  之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据。

  like this:

select * from 
(
    select row_number() over(order by LogID desc) as rnum,* from  B_ShortProtectLog where addtime>=2014-9-1 and addtime<2014-10-1
) as tLog  where rnum between 1 and 20;

 

  但是对于大表查询的话如果用ROW_NUMBER这里应该使用ROW_NUMBER的SELECT只查询出主键就可以了,其他的信息取出分页数据的主键之后,连表查询就可以了。

  类似:

with tempLog as
(
    select row_number() over(order by LogID desc) as rnum,LogID from B_ShortProtectLog where addtime>=2014-9-1 and addtime<2014-10-1
) select tLog.* from tempLog left join B_ShortProtectLog as tLog on tempLog.LogID=tLog.LogID where rnum between 50001 and 50020;

 

ROW_NUMBER分页的错误使用

标签:

原文地址:http://www.cnblogs.com/mylove-programming/p/4283392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!