日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁西西教程數(shù)據(jù)庫教程 → oracle分頁所遇到的rownum問題:要增加order by的唯一性

      oracle分頁所遇到的rownum問題:要增加order by的唯一性

      相關(guān)軟件相關(guān)文章發(fā)表評論 來源:西西整理時(shí)間:2012/11/28 14:01:49字體大。A-A+

      作者:sqtds點(diǎn)擊:0次評論:0次標(biāo)簽: 分頁

      • 類型:濾鏡插件大。3.7M語言:英文 評分:5.0
      • 標(biāo)簽:
      立即下載

      昨天做完項(xiàng)目后讓測試測試了一把,測試說分頁查詢貌似不起作用,翻到第4頁以后,看到的數(shù)據(jù)結(jié)果都是相同的。

      當(dāng)時(shí)我就覺得很納悶,不可能啊,分頁組件應(yīng)該是好的,咋可能有問題呢。帶著疑問,我打開了自己的ide,在自己的機(jī)器上跑了一把,果然有問題。

      有問題就要找問題:

      首先把2條查詢結(jié)果相同的sql打印出來到數(shù)據(jù)庫中執(zhí)行:

      sql1:
      select *
      from (select t.*, rownum rn
      from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
      from (select t.eid t_e_id,
      t.ename t_e_name,
      t.etel t_e_tel,
      t.areaid t_e_areacode,
      t.biz_delete_time,
      decode(areaid, '0730', '0', '1') orderseq
      from vr_enterprise t
      where t.eid not in (select eid from t_biz_erelation))
      order by orderseq, biz_delete_time nulls last) t
      where rownum < 25)
      where rn >= 19

      sql2:
      select *
      from (select t.*, rownum rn
      from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
      from (select t.eid t_e_id,
      t.ename t_e_name,
      t.etel t_e_tel,
      t.areaid t_e_areacode,
      t.biz_delete_time,
      decode(areaid, '0730', '0', '1') orderseq
      from vr_enterprise t
      where t.eid not in (select eid from t_biz_erelation))
      order by orderseq, biz_delete_time nulls last) t
      where rownum <18)
      where rn >= 12

      結(jié)果顯示大多數(shù)行是相同的。

      為了找到問題所在,只有先一步一步的精簡化sql,看在哪一步出的問題。

      于是找到了,問題出現(xiàn)在where rownum<18的時(shí)候數(shù)據(jù)改變了,為什么加了個(gè)where條件結(jié)果就會變呢?

      表示想不通啊。。。。。

      沒辦法,只好baidu了,baidu了半天,都沒人給個(gè)解釋啊。。。。。

      后來同事說,換個(gè)寫法試試,于是改了另一種寫法,如下:
      select *
      from (select t.*, rownum rn
      from (select t_e_id, t_e_name, t_e_tel, t_e_areacode
      from (select t.eid t_e_id,
      t.ename t_e_name,
      t.etel t_e_tel,
      t.areaid t_e_areacode,
      t.biz_delete_time,
      decode(areaid, '0730', '0', '1') orderseq
      from vr_enterprise t
      where t.eid not in (select eid from t_biz_erelation))
      order by orderseq, biz_delete_time nulls last) t)m
      where m.rn >= 1 and m.rn <25

      這個(gè)方法果然湊效,湊效是湊效,關(guān)鍵是為什么前面那種方法不行呢?抓問題要刨根問底。

      看來baidu不行,得換google搜索了。找呀找找呀找,在oracle的ask tom上,tom詳細(xì)的介紹了rownum的用法,在這里http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html

      One important thing about using this pagination query is that the ORDER BY statement should order by something unique. If what you are ordering by is not unique, you should add something to the end of the ORDER BY to make it so.

      看到這樣一句話,原來用order by的時(shí)候還需要增加order by的唯一性,不然rownum就會有問題。哇哈哈哈,太開心了,功夫不費(fèi)有心人呀,找到問題的癥結(jié)了,當(dāng)然就好解決了。

      這權(quán)當(dāng)是一次排錯(cuò)的經(jīng)歷,特此記錄下來,看來google還是比baidu強(qiáng)大很多啊,以后要好好學(xué)英文,不然遇到問題沒發(fā)解決。

        相關(guān)評論

        閱讀本文后您有什么感想? 已有人給出評價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過難過
        • 5 囧
        • 3 圍觀圍觀
        • 2 無聊無聊

        熱門評論

        最新評論

        發(fā)表評論 查看所有評論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)