Categories


Tags


三种数据库利用SQL语句进行高效果分页

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

3、MySQL数据库

Mysql可以使用 limit子句实现

select * from tablename limit start,number

返回从第 start+l条记录开始的number条记录。

来源:赛迪网

来源:月光博客


Public @ 2010-02-01 15:47:53

macbook苹果电脑系统使用“终端”远程登录linux主机

要在Macbook上使用“终端”远程登录Linux主机,可以使用以下步骤: 1. 打开“终端”应用程序。可以在“应用程序”文件夹中找到它,或者使用Spotlight搜索。 2. 输入以下命令: $ ssh 用户名@主机名 其中,用户名是您在Linux主机上的用户名,主机名是Linux主机的域名或IP地址。 如果您第一次连接该主机,会提示您确认主机的RSA密钥指纹。输入“yes”并按回车键

Public @ 2023-06-23 11:50:11

保留数据重装后sqlserver数据库不能启动(sql2008,sql2012)

最近有很多用户反馈重装为预装sqlserver模板的系统,sqlserver不能正常启动,经核实大部分都是用户保留数据重装更换另外模板的windows系统的原因。由于数据库安装在D盘,有些模板是没有安装sqlserver数据库的,重装前D盘并没有对应sqlserver版本的数据,而保留数据重装D盘的数据不会被替换,所以数据库不能启动。以下是处理方法处理前请先注意以下事项:1、如果【D:\Progr

Public @ 2023-01-18 15:47:49

更多您感兴趣的搜索

0.416177s