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

  亲爱的小伙伴们,我们今天要讲解的内容是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;
 
   
 
 
  
 
 

 

       

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值