Oracle、SQL Server、Access数据库高效果分页技巧


1、SQL Server、Access数据库

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

  PAGESIZE:每页显示的记录数

  CURRENTPAGE:当前页号

  数据表的名字是:components

  索引主键字是:id

以下是引用片段:
selecttopPAGESIZE*fromcomponentswhereidnotin
(selecttop(PAGESIZE*(CURRENTPAGE-1))
idfromcomponentsorderbyid)orderbyid

  如下列:

以下是引用片段:
selecttop10*fromcomponentswhereidnotin
(selecttop10*10idfromcomponentsorderbyid)
orderbyid

  从101条记录开始选择,只选择前面的10条记录

  2、Oracle数据库

  因为Oracle数据库没有Top关键字,所以这里就不能够像微软的数据据那样操作,这里有两种方法:

  (1)、一种是利用相反的。

  PAGESIZE:每页显示的记录数

  CURRENTPAGE:当前页号

  数据表的名字是:components

  索引主键字是:id

以下是引用片段:
select*fromcomponentswhereidnot
in(selectidfromcomponentswhere
rownum<=(PAGESIZE*(CURRENTPAGE-1)))
andrownum<=PAGESIZEorderbyid;

  如下例:

以下是引用片段:
select*fromcomponentswhereidnotin
(selectidfromcomponentswhererownum<=100)
andrownum<=10orderbyid;

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

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

以下是引用片段:
select*fromcomponentswhererownum
<=(PAGESIZE*(CURRENTPAGE-1))minus
select*fromcomponentswhererownum
<=(PAGESIZE*(CURRENTPAGE-2));

  如例:select * from components where

以下是引用片段:
rownum<=10minusselect*fromcomponents
whererownum<=5;.

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

以下是引用片段:
select*from(selectrownumtid,components.
*fromcomponentswhererownum<=100)wheretid<=10;

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3