数据库常见分页的处理方式

数据库常见分页的处理方式

  亲爱的小伙伴们,我们今天要讲解的内容是Java的分页的相关内容,这里主要讲的是如何控制数据查询出指定记录的信息。

至于分页的具体方式,这里不做过多的介绍。感兴趣的话可以自己研究。

一.使用数据本身的分页方式进行相关分页操作

    1.sql server数据的方案

        方案一:

        -- pager the record

     -- note: n代表要求查询的第n页的记录,x表示每一页有x条记录。
     select top x * from tb
     where pk_col not in (select top (n-1)*x pk_col from tb) 
select top x * from tb where pk_col not in (select top (n-1)*x
     -- demo(示例:)查询第31到40页的信息
    select top 10 * from persons
    where username not in(select top ((3-1)*10) username from persons)

方案二:
 -- pager the record
   -- note: n代表要求查询的第n页的记录,x表示每一页有x条记录。

select top x * from tb
where pk_col in (select top (n-1)*x pk_col from tb order by pk_col asc)order by pk_col desc;
 -- demo(示例:)查询第31到40页的信息
select top 10 * from tb where pk_col in (select top (3-1)*10 pk_col from tb order by pk_col asc) order by pk_col desc;

2.Mysql数据库的分页方案
MySQL数据库实现分页比较简单,提供了 LIMIT函数。一般只需要直接写到sql语句后面就行了。
LIMIT子 句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数,如果给出两个参数,
第一个参数指定返回的第一行在所有数据中的位置,从0开始(注意不是1),第二个参数指定最多返回行数。
例如: select * from table WHERE … LIMIT 10; #返回前10行
select * from table WHERE … LIMIT 0,10; #返回前10行
select * from table WHERE … LIMIT 10,20; #返回第10-20行数据

--demo
查询31到40的结果集
select * from tb limit 30,10;
 
3.Oracle数据的分页方案
考虑mySql中的实现分页,select * from 表名  limit 开始记录数,显示多少条;就可以实现我们的分页效果。
但是在oracle中没有limit关键字,但是有 rownum字段rownum是一个伪列,是oracle系统自动为查询返回结果的
每行分配的编号,第一行为1,第二行为2,以此类推。。。。

例如:select * from (select rownum rn,* from tb where rn<=40) where rn>30;
--demo
    查询31到40的结果集
    select * from tb (select rownum rn,* from tb where rn<=40) where rn>30;