数据库分页查询

MYSQL分页公式

(当前页数-1) * 每页条数,每页条数

SELECT 
  * 
FROM
  TABLE 
LIMIT (Page - 1) * PageSize, PageSize ;

SQLSERVER

(row_number()over(order by))

该关键字只有在SQL server 2005版本以上才有

-- 分页查询公式-row_number()-优化版本
select * 
from (
    -- rownumber是别名,可按自己习惯取
    select top (Page*PageSize) row_number() over(order by 主键 asc) 
    as rownumber,* 
    from 表名) temp_row
where rownumber>((Page-1)*PageSize);

 ORCLE

SELECT * FROM (
    SELECT ROWNUM r, tab.* FROM tab_name tab
        WHERE ROWNUM <= pageSize * Page ) temp
WHERE r > pageSize * Page  - pageSize;

SELECT *  FROM (
    SELECT ROWNUM rn, temp.* FROM (
        SELECT * FROM tab_name tab ORDER BY tab.pk_column DESC) temp
        WHERE ROWNUM < end_num)
WHERE rn > start_num;

 

posted @ 2022-12-06 15:04  RC城  阅读(70)  评论(0编辑  收藏  举报