利用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
投稿

猜你喜欢

  • Zend Framework生成验证码并实现验证码验证功能(附demo源码下载)

    2024-05-03 15:13:30
  • 使用matplotlib库实现图形局部数据放大显示的实践

    2021-01-13 18:47:13
  • C# 制作PictureBox圆形头像框并从数据库中读取头像

    2024-01-21 05:17:07
  • Python 识别录音并转为文字的实现

    2021-12-05 02:40:28
  • 在Docker上部署Python的Flask框架的教程

    2023-03-27 02:55:38
  • 使用pyshp包进行shapefile文件修改的例子

    2023-07-01 08:28:35
  • python下如何让web元素的生成更简单的分析

    2023-01-28 23:46:34
  • sql表连接查询使用方法(sql多表连接查询)

    2024-01-22 12:25:39
  • MySQL安全性指南(3)(转)

    2010-07-26 13:07:00
  • 脚本安全的本质_PHP+MYSQL第1/3页

    2023-11-23 23:54:45
  • Python入门之布尔值详解

    2023-01-17 06:29:58
  • python selenium UI自动化解决验证码的4种方法

    2022-10-09 20:43:06
  • kafka rabbitMQ及rocketMQ队列的消息可靠性保证分析

    2022-05-06 08:10:23
  • PYQT5实现控制台显示功能的方法

    2023-08-08 11:11:53
  • IE不支持overrideMimeType()方法,即使是IE7.

    2009-02-08 16:58:00
  • 重命名SQLServer数据库的方法

    2012-07-11 15:39:37
  • Python读取excel指定列生成指定sql脚本的方法

    2021-05-06 19:11:13
  • python实现感知器算法详解

    2022-04-29 15:08:43
  • VS 2008的性能改进

    2007-10-07 21:42:00
  • python实现矩阵乘法

    2023-11-03 07:41:10
  • asp之家 网络编程 m.aspxhome.com