hive sql 翻頁

Hive 提供了類似 Oracle 的 rownum 機制,類似這樣(效率比較差):

select * from (select row_number() over (order by create_time desc) as rownum,u.* from user u) mm where mm.rownum between 10 and 15;
between 和 and 分頁可以按照下面這個公式 between :( currentPage- 1)pageSize+1 ,and : (currentPagepageSize)

還有一種辦法,如果表里有唯一標識字段也可以借助這個字段和 limit 實現(xiàn)。比如:
獲取第一頁數(shù)據(jù):
注:同時需要記錄這 10 條中最大的 id 為 preId,作為下一頁的條件。

select * from table order by id asc limit 10;

獲取第二頁數(shù)據(jù):
注:同時保存數(shù)據(jù)中最大的 id 替換 preId。

select * from table where id >preId order by id asc limit 10;

舉例:

獲取第一頁數(shù)據(jù):

select * from table where type=2 order by id asc limit 10;

獲取第二頁數(shù)據(jù):

需要獲取第一頁10條中最大的id為preId,作為下一頁的條件。

int preId=select max(id) from table where type=2 order by id asc limit 10;

select * from table where type=2 and id >preId order by id asc limit 10;

獲取第三頁數(shù)據(jù):

需要獲取2頁20條中最大的id為preId,作為下一頁的條件。

int preId=select max(id) from table where type=2 order by id asc limit 20;

select * from table where type=2 and id >preId order by id asc limit 10;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Hive 提供了類似 Oracle 的 rownum 機制,類似這樣(效率比較差): select * from ...
    心宇gxy閱讀 1,053評論 0 0
  • 一、數(shù)據(jù)庫概述 什么是數(shù)據(jù)庫數(shù)據(jù)庫就是存儲數(shù)據(jù)的倉庫,其本質(zhì)是一個文件系統(tǒng),數(shù)據(jù)按照特定的格式將數(shù)據(jù)存儲起來,用戶...
    圣賢與無賴閱讀 4,061評論 0 4
  • 首先需要注意的是:查詢語句select再怎么操作都不會改變表中的數(shù)據(jù)!! 1、查詢語句的語法格式 select 列...
    jimmywife閱讀 2,291評論 0 4
  • mysql分頁: 從0開始計數(shù) 第0頁: select * from student limit 0,10; 第1...
    quixotic閱讀 106評論 0 1
  • 久違的晴天,家長會。 家長大會開好到教室時,離放學已經(jīng)沒多少時間了。班主任說已經(jīng)安排了三個家長分享經(jīng)驗。 放學鈴聲...
    飄雪兒5閱讀 7,787評論 16 22

友情鏈接更多精彩內(nèi)容