oracle 分頁問題解決方案
更新時間:2012年11月28日 14:10:38 作者:
測試說分頁查詢貌似不起作用,翻到第4頁以后,看到的數(shù)據(jù)結(jié)果都是相同的,此等問題應該如何解決,本文將詳細介紹
昨天做完項目后讓測試測試了一把,測試說分頁查詢貌似不起作用,翻到第4頁以后,看到的數(shù)據(jù)結(jié)果都是相同的。
當時我就覺得很納悶,不可能啊,分頁組件應該是好的,咋可能有問題呢。帶著疑問,我打開了自己的ide,在自己的機器上跑了一把,果然有問題。
有問題就要找問題:
首先把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ù)據(jù)改變了,為什么加了個where條件結(jié)果就會變呢?
表示想不通啊。。。。。
沒辦法,只好baidu了,baidu了半天,都沒人給個解釋啊。。。。。
后來同事說,換個寫法試試,于是改了另一種寫法,如下:
復制代碼 代碼如下:
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
這個方法果然湊效,湊效是湊效,關(guān)鍵是為什么前面那種方法不行呢?抓問題要刨根問底。
看來baidu不行,得換google搜索了。google搜索總是喜歡被china government墻掉,沒辦法,網(wǎng)上只好找了個chrome插件才解決了。
找呀找找呀找,在oracle的ask tom上,tom詳細的介紹了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的時候還需要增加order by的唯一性,不然rownum就會有問題。哇哈哈哈,太開心了,功夫不費有心人呀,找到問題的癥結(jié)了,當然就好解決了。
這權(quán)當是一次排錯的經(jīng)歷,特此記錄下來,看來google還是比baidu強大很多啊,以后要好好學英文,不然遇到問題沒發(fā)解決。
相關(guān)文章
解決Oracle19c?ORA-00904:“WMSYS“.“WM_CONCAT“:標識符無效問題
這篇文章主要介紹了解決Oracle19c?ORA-00904:“WMSYS“.“WM_CONCAT“:標識符無效問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
PLSQL Developer連接Oracle11g 64位數(shù)據(jù)庫配置詳解(圖文)
這篇文章主要介紹了PLSQL Developer連接Oracle11g 64位數(shù)據(jù)庫配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
在oracle 數(shù)據(jù)庫查詢的select 查詢字段中關(guān)聯(lián)其他表的方法
在實際開發(fā)過程中,特別是在做數(shù)據(jù)查詢的時候,能夠根據(jù)動態(tài)生成的sql語句將查詢的結(jié)果轉(zhuǎn)化并返回到業(yè)務(wù)處理邏輯(或頁面展示出來)能大大的減輕業(yè)務(wù)邏輯的處理復雜度。2009-08-08

