Oracle与MySQL的分页查询sql语句格式实例讲解
程序员文章站
2023-10-27 21:36:28
Oracle
Oracle使用rownum进行分页:
SELECT *
FROM (SELECT a.*, ROWNUM rn
FROM (SELECT *...
Oracle
Oracle使用rownum进行分页:
SELECT * FROM (SELECT a.*, ROWNUM rn FROM (SELECT * FROM table_name) a WHERE ROWNUM <= pageIndex * pageSize) WHERE rn > (pageIndex - 1) * pageSize
其中,pageIndex是页码,pageSize是每页的条数
MySQL
MySQL的limit语句是很方便,但是对数据量很大的表不适合直接使用。
综合考虑下,我们可以利用索引来进行高效地分页查询
SELECT * FROM table_name WHERE id > =(SELECT id from table_name limit (pageIndex-1) * pageSize, 1) limit pageSize
或
SELECT * FROM table_name a JOIN (select id from table_name limit (pageIndex-1) * pageSize, pagesize) b ON a.id = b.id