Oracle、SQL Server、Access利用SQL语句进行高效果分页

在程序开发中,处理分页往往是比较频繁的事件,因为现在软件基本上都是与数据库进行挂钓的。但效率又是我们所追求的,如果是像原来那样把所有满足条件的记录全部都选择出来,再去进行分页处理,那么就会多多的浪费掉许多的系统处理时间。为了能够把效率提高,所以现在我们就只选择我们需要的数据,减少数据库的处理时间,以下就是常用SQL分页处理:
    1、SQL Server、Access数据库
    这都微软的数据库,都是一家人,基本的操作都是差不多,常采用如下分页语句:
    PAGESIZE:每页显示的记录数
    CURRENTPAGE:当前页号
    数据表的名字是:components
    索引主键字是:id
Sql代码 复制代码
  1. select top PAGESIZE * from components where id not in  
  2. (select top (PAGESIZE*(CURRENTPAGE-1)) id from components order by id)   
  3. order by id  
    select top PAGESIZE * from components where id not in
    (select top (PAGESIZE*(CURRENTPAGE-1)) id from components order by id)
    order by id

    如下列:
Sql代码 复制代码
  1. select top 10 * from components where id not in  
  2. (select top 10*10 id from components order by id)   
  3. order by id  
    select top 10 * from components where id not in
    (select top 10*10 id from components order by id)
    order by id

    从101条记录开始选择,只选择前面的10条记录
    2、Oracle数据库
    因为Oracle数据库没有Top关键字,所以这里就不能够像微软的数据据那样操作,这里有两种方法:
    1)、一种是利用相反的。 
    PAGESIZE:每页显示的记录数
    CURRENTPAGE:当前页号
    数据表的名字是:components
    索引主键字是:id
Sql代码 复制代码
  1. select * from components where id not in(select id from components where  rownum<=(PAGESIZE*(CURRENTPAGE-1))) and rownum<=PAGESIZE order by id;  
    select * from components where id not in(select id from components where  rownum<=(PAGESIZE*(CURRENTPAGE-1))) and rownum<=PAGESIZE order by id;

    如下例:
Sql代码 复制代码
  1. select * from components where id not in(select id from components where rownum<=100) and rownum<=10 order by id;  
    select * from components where id not in(select id from components where rownum<=100) and rownum<=10 order by id;

    从101到记录开始选择,选择前面10条。
    2)、使用minus,即中文的意思就是减去,呵呵,这语句非常的有意思,也非常好记
Sql代码 复制代码
  1. select * from components where rownum<=(PAGESIZE*(CURRENTPAGE-1)) minus select * from components where rownum<=(PAGESIZE*(CURRENTPAGE-2));  
    select * from components where rownum<=(PAGESIZE*(CURRENTPAGE-1)) minus select * from components where rownum<=(PAGESIZE*(CURRENTPAGE-2));

     如例:
Sql代码 复制代码
  1. select * from components where rownum<=10 minus select * from components where rownum<=5;.  
select * from components where rownum<=10 minus select * from components where rownum<=5;.

    3)、一种是利用Oracle的rownum,这个是Oracle查询自动返回的序号,一般不显示,但是可以通过select rownum from [表名],可以看到,是从1到当前的记录总数。 
   
Sql代码 复制代码
  1. select * from (select rownum tid,components.* from components where rownum<=100) where tid<=10;  
select * from (select rownum tid,components.* from components where rownum<=100) where tid<=10;

    Oracle毕竟还是大家闺秀,微软的小家碧玉还是差一点,呵呵,自己选择着用吧。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值