一、問題描述:根據更新時間倒序排序然后分頁查詢數據,但是點擊分頁操作的時候,會出現數據重復看似沒有操作的情況
二、問題錯誤原因分析
分頁查詢的SQL語句:
1 1 select * 2 2 FROM (select rownum rn,tmp_tb.* 3 3 FROM (select * FROM DB_ENTERPRISE WHERE 1=1 and del_flag='0' order by UPDATE_TIME desc) tmp_tb 4 4 WHERE rownum <= 1) 5 5 WHERE rn >= 10;
怎么分析都覺得沒有錯啊,通過網上查詢才發現問題所在,參考地址:https://jingyan.baidu.com/article/7908e85ca2d929af491ad260.html
由於我要排序的字段有大量導入數據的時間是一樣的,所以導致無法區分
三、解決辦法
給排序的字段再加一個唯一字段進行排序,即可
更新SQL語句:
1 select u.* 2 FROM (select rownum rn,tmp_tb.* 3 FROM (select id, enterprise_name, enterprise_type, enterprise_domain, enterprise_province, enterprise_address, create_user_id, create_user_name, create_time, update_time, del_flag, ext1, ext2, ext3, ext4, ext5,ENTERPRISE_PROVINCE_ID 4 FROM DB_ENTERPRISE 5 WHERE 1=1 and del_flag='0' order by UPDATE_TIME desc,id) tmp_tb 6 WHERE rownum <= 21 )u 7 WHERE u.rn >= 10 ;