一分钟搞懂mysql、oracle、sql server分页查询

    xiaoxiao2021-04-15  32

    假设表db_table里有100条记录,每页展示十条记录,现在分别用mysql、oracle、sql server实现分页查询第二页记录。 mysql:

    /** * select * from table limit firstIndex,pageSize * 说明:limit后第一个参数为起始下标,第二个参数为需要查询的记录数 * firstIndex:起始下标(从0开始) * pageSize:每页展示的记录数 */ select t.* from tb_table t limit 10,10;

    oracle:

    /** * 方法一:select * from ( select t.*,rownum rn from tb_table t where rownum <=(pageNo * pageSize) ) where rn > ((pageNo-1) * pageSize) * 说明:oracle自带rownum记录行信息,但是只能查询出<=rownum的数据,因此需要用该法实现分页。 * pageNo:需要查询的页数 * pageSize:每页展示的记录数 */ select * from ( select t.*,rownum rn from tb_table t where rownum <=20 ) where rn > 10; /** * 方法二:select * from ( select t.*,rownum rn from tb_table t ) where rn between firstRowNumber and endRowNumber; * firstRowNumber:起始行(从1开始) * endRowNumber:结束行(从1开始) */ select * from ( select t.*,rownum rn from tb_table t ) where rn between 11 and 20;

    sql server:

    /** * select top pageSize t.* from tb_table t where id not in ( select top firstRowNumber id from tb_table ) * firstRowNumber:起始行(从1开始) * pageSize:每页展示的记录数 */ select top 10 t.* from tb_table t where id not in ( select top 10 id from tb_table );

    总结,以上只是最简单最基础最顺手的分页查询,但实际开发过程中需要结合实际情况进行sql编写。

    转载请注明原文地址: https://ju.6miu.com/read-671982.html

    最新回复(0)