關於SQL分頁計算公式


 
        

1、mysql獲取部分行(limit)語法

代碼:

-- 從start開始,獲取count條數據
-- start索引從0開始

select * from 表名 limit start,count 

-- 查詢前數據庫從0開始的count條記錄

select * from 表名 limit count

2、oracle獲取部分行(rownum)語法

代碼:

-- 從start開始,獲取到第end條結束
-- start索引從0開始

select * from 表名 rownum>start and rownum<=end

3、java分頁獲取數據集

代碼:

已知:每頁顯示條數(pageSize),求顯示第頁的數據(currPage) 

//mysql
String sql = new String("select * from 表名 limit(currPage-1)*pageSize, currPage");

//oracle
int start = (currPage - 1) * pageSize;
int end = currPage * pageSize;
String sql = new String(select * from 表名 rownum>start and rownum<=end");

//執行並獲取
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.execute();
ResultSet rs = preparedStatement.getResultSet();



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM