利用SQL语句对不同数据库进行高效果分页

时间:2008-11-28 14:44:00 

在程序的开发过程中,处理分页是大家接触比较频繁的事件,因为现在软件基本上都是与数据库进行挂钩的。但效率又是我们所追求的,如果是像原来那样把所有满足条件的记录全部都选择出来,再去进行分页处理,那么就会多多的浪费掉许多的系统处理时间。为了能够把效率提高,所以现在我们就只选择我们需要的数据,减少数据库的处理时间,以下就是常用SQL分页处理:

1、SQL Server、Access数据库

这都微软的数据库,都是一家人,基本的操作都是差不多,常采用如下分页语句:

PAGESIZE:每页显示的记录数

CURRENTPAGE:当前页号

数据表的名字是:components

索引主键字是:id

select top PAGESIZE * from components where id not in

(select top (PAGESIZE*(CURRENTPAGE-1))

id from components order by id)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

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<=100)

and rownum<=10 order by id;

从101到记录开始选择,选择前面10条。


(2)、使用minus,即中文的意思就是减去。

select * from components where rownum

<=(PAGESIZE*(CURRENTPAGE-1)) minus

select * from components where rownum

<=(PAGESIZE*(CURRENTPAGE-2));

如例:select * from components where

rownum<=10 minus select * from components

where rownum<=5;.

(3)、一种是利用Oracle的rownum,这个是Oracle查询自动返回的序号,一般不显示,但是可以通过select rownum from [表名]看到,注意,它是从1到当前的记录总数。

select * from (select rownum tid,components.

* from components where rownum<=100) where tid<=10;

标签:
0
投稿

猜你喜欢

  • 打败 IE 的葵花宝典:CSS Bug Table

    2010-08-03 12:30:00
  • DTS构建组件及其如何完成数据转换服务

    2009-01-20 15:37:00
  • SQL语句分组获取记录的第一条数据的方法

    2012-08-21 10:58:39
  • 利用aspjpeg组件自动生成产品缩略图asp源代码

    2007-08-10 10:14:00
  • Mootools 1.2教程(20)——选项卡效果(Tabs)

    2008-12-26 18:19:00
  • word-wrap同word-break的区别

    2007-10-24 20:08:00
  • Silverlight 3 Beta出来了

    2009-03-19 13:17:00
  • FrontPage2002简明教程四:网页超级链接

    2008-09-17 11:23:00
  • 用Dreamweaver设计Wordpress留言板教程(一)

    2010-03-17 15:44:00
  • SQL语言基本语句介绍

    2008-07-24 13:32:00
  • 查看ASP详细错误提示信息的图文设置方法

    2011-02-05 11:02:00
  • 用CSS实现柱状图(Bar Graph)的方法(四)—table实现复杂柱状图

    2008-05-28 12:55:00
  • 如何从ASP连接到Oracle Server?

    2009-11-15 19:52:00
  • Asp中通过简单的例子理解下ByVal和ByRef的用法

    2011-02-20 10:57:00
  • oracle 动态AdvStringGrid完美示例 (AdvStringGrid使用技巧/Cells)

    2009-06-19 17:21:00
  • Yahoo! BrowserPlus 介绍

    2008-06-01 16:38:00
  • my sql存储过程学习总结

    2011-07-12 19:12:35
  • mysql到oracle的移植

    2011-01-29 16:23:00
  • 腾讯网QQ首页诞生的艰辛历程

    2008-11-06 11:05:00
  • Ajax改造:使用Ajax和jQuery改进现有站点

    2010-04-02 12:50:00
  • asp之家 网络编程 m.aspxhome.com