oracle rownum對排序的影響

1. 標準的rownum分頁查詢使用方法:

    select *
      from (select c.*, rownum rn from content c)
     where rn >= 1
       and rn <= 5 優化


    2. 可是若是, 加上order by addtime 排序則數據顯示不正確 排序

    select *
      from (select c.*, rownum rn from content c order by addtime)
     where rn >= 1
       and rn <= 5 it


    解決方法,再加一層查詢,則能夠解決, 效率

    select *
      from (select rownum rn, t.*
              from (select title, addtime from content order by addtime desc) t)
     where rn >= 1
       and rn <= 5 select

    若是要考慮到效率的問題,上面的還能夠優化成(主要二者區別) 分頁

    select *
      from (select rownum rn, t.*
              from (select title, addtime from content order by addtime desc) t
             where rownum <= 10)
     where rn >= 3 方法

相關文章
相關標籤/搜索