Oracle实现分页查询的方式(附带mysql分页)

1 引言

Oracle中的rownum一般是在实现分页查询时用到,虽然在我做的项目中只实现了分页显示而没有实现真正的分页,但是出于学习的目的研究了一下oraclerownum。在使用查询语句时,我们经常要求返回表中的前n条记录或者是中间的几条记录,比如在一个大表(假设有10W条数据)要求查询从第10001005条的记录。面对这种查询,我们怎么办呢?mysqloracle都有自己的解决办法。

2 MySql中的实现

mysql中,我们可以使用limit语句来实现:

1)查询从第10001005条的记录(注意mysql中的记录是从0开始编号的,所以第1000条记录编号为999

select * from table_name limit 999,5;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

2)查询前10条记录

select * from table_name limit 10;

等价于:

select * from table_name limit 0,10;

2)查询从第100条记录开始到表的最后一条记录

select * from table_name limit 99 -l;

mysql提供-L的参数,表示到表的最后一条记录

3 Oracle中的实现

Oracle使用rownum的关键字来实现这种查询:

首先我们假设有一个地域信息表area,其表结构如下图所示:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

表中的数据如下图所示(select * from area语句得到的结果):

 

1)查询表中的前8条记录
select * from area where rownum <= 8
查询结果如下:

 


2)查询第2到第8条记录
对于这种形式的查询,oracle不像mysql那么方便,它必须使用子查询或者是集合操作来实现。我们可以使用以下3种方式可以实现:

A: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num between 2 and 8;

首先根据select id,province,city,district,rownum as num from area得到一个临时表,这个临时表中有一个rownum列(一个伪列,类似与rowid,但又不同于rowid,因为rowid是物理存在的一个列,也就是说Oracle数据库中任何一个表都有一个rowid列,而rownum不是物理存在的),然后在临时表中来查询。

B: select * from area where rownum <= 8 minus select * from area where rownum < 2;

使用集合减运算符minus,该操作返回在第一个select中出现而不在第二个select中出现的记录。

C: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num >=2

intersect

select * from area where rownum <= 8;

使用集合交运算符intersect,这里绕了一个弯(不过这个弯实现了rownum大于某个数的查询),它是首先利用A的方式查询得到所有rownum大于2的记录,然后再与rownum小于等于8的记录集合做交运算。三种操作得到的结果一样,如下图所示:

 


3rownum需要注意的问题

[1] rownum不支持以下方式的查询

a: select * from area where rownum > 2;

b: select * from area where rownum = n;  --where n is a integer number lager than 1

注:rownum只支持select * from area where rownum =1的查询。Oracle的官方文档说明如下:

Conditions testing for ROWNUM values greater than a positive integer are always false.

For example, this query returns no rows:

SELECT * FROM employees

WHERE ROWNUM > 1;

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The

second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and

makes the condition false. All rows subsequently fail to satisfy the condition, so no

rows are returned.

因为rownum是根据查询的结果集来对记录进行编号,所以当你查询rownum大于2的记录时会得到一个空的结果集。因为当oracle查询得到第1条记录时,发现rownum1不满足条件,然后就继续查询第2条记录,但此时第2条记录又被编号为1(也即rownum变为1),所以查询得到的始终是rownum=1,因此无法满足约束,最终查询的结果集为空。

 

[2] rownum的排序查询问题

Rownum的排序查询是根据表中数据的初始顺序来进行的。Oracle官方文档中说明如下:

If an ORDER BY clause follows ROWNUM in the same query, then the rows will be

reordered by the ORDER BY clause. The results can vary depending on the way the

rows are accessed. For example, if the ORDER BY clause causes Oracle to use an index

to access the data, then Oracle may retrieve the rows in a different order than without

the index.

例如:select * from area where rownum <= 8 order by district;

其结果如下图所示:

 

发现没有,它只对area表中的前8条记录进行排序。那么,如果我要取表中的前8条记录并且要求是全表有序,那怎么办呢?还是老办法,使用子查询。我们可以使用以下语句得到:

select * from (select * from area order by district)

where rownum <= 8;

查询的结果如下图所示:

 

4 结束语

Oracle中的rownummysqllimit实现的功能相同,但没有mysql来的容易,它一般通过一个子查询来实现。mysql的易用性也是它能够纵横开源数据库的原因,它不像postgresql那样的学院派,它的那种简单易用性或许在大型软件项目的开发中值得借鉴。最近听说sql server 2008也实现了limit的查询,不过还没去试过,Oracle在这方面也要加油啊,用户容易使用才是王道。

1 引言

Oracle中的rownum一般是在实现分页查询时用到,虽然在我做的项目中只实现了分页显示而没有实现真正的分页,但是出于学习的目的研究了一下oraclerownum。在使用查询语句时,我们经常要求返回表中的前n条记录或者是中间的几条记录,比如在一个大表(假设有10W条数据)要求查询从第10001005条的记录。面对这种查询,我们怎么办呢?mysqloracle都有自己的解决办法。

2 MySql中的实现

mysql中,我们可以使用limit语句来实现:

1)查询从第10001005条的记录(注意mysql中的记录是从0开始编号的,所以第1000条记录编号为999

select * from table_name limit 999,5;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

2)查询前10条记录

select * from table_name limit 10;

等价于:

select * from table_name limit 0,10;

2)查询从第100条记录开始到表的最后一条记录

select * from table_name limit 99 -l;

mysql提供-L的参数,表示到表的最后一条记录

3 Oracle中的实现

Oracle使用rownum的关键字来实现这种查询:

首先我们假设有一个地域信息表area,其表结构如下图所示:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

表中的数据如下图所示(select * from area语句得到的结果):

 

1)查询表中的前8条记录
select * from area where rownum <= 8
查询结果如下:

 


2)查询第2到第8条记录
对于这种形式的查询,oracle不像mysql那么方便,它必须使用子查询或者是集合操作来实现。我们可以使用以下3种方式可以实现:

A: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num between 2 and 8;

首先根据select id,province,city,district,rownum as num from area得到一个临时表,这个临时表中有一个rownum列(一个伪列,类似与rowid,但又不同于rowid,因为rowid是物理存在的一个列,也就是说Oracle数据库中任何一个表都有一个rowid列,而rownum不是物理存在的),然后在临时表中来查询。

B: select * from area where rownum <= 8 minus select * from area where rownum < 2;

使用集合减运算符minus,该操作返回在第一个select中出现而不在第二个select中出现的记录。

C: select id,province,city,district from (select id,province,city,district,rownum as num from area) where num >=2

intersect

select * from area where rownum <= 8;

使用集合交运算符intersect,这里绕了一个弯(不过这个弯实现了rownum大于某个数的查询),它是首先利用A的方式查询得到所有rownum大于2的记录,然后再与rownum小于等于8的记录集合做交运算。三种操作得到的结果一样,如下图所示:

 


3rownum需要注意的问题

[1] rownum不支持以下方式的查询

a: select * from area where rownum > 2;

b: select * from area where rownum = n;  --where n is a integer number lager than 1

注:rownum只支持select * from area where rownum =1的查询。Oracle的官方文档说明如下:

Conditions testing for ROWNUM values greater than a positive integer are always false.

For example, this query returns no rows:

SELECT * FROM employees

WHERE ROWNUM > 1;

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The

second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and

makes the condition false. All rows subsequently fail to satisfy the condition, so no

rows are returned.

因为rownum是根据查询的结果集来对记录进行编号,所以当你查询rownum大于2的记录时会得到一个空的结果集。因为当oracle查询得到第1条记录时,发现rownum1不满足条件,然后就继续查询第2条记录,但此时第2条记录又被编号为1(也即rownum变为1),所以查询得到的始终是rownum=1,因此无法满足约束,最终查询的结果集为空。

 

[2] rownum的排序查询问题

Rownum的排序查询是根据表中数据的初始顺序来进行的。Oracle官方文档中说明如下:

If an ORDER BY clause follows ROWNUM in the same query, then the rows will be

reordered by the ORDER BY clause. The results can vary depending on the way the

rows are accessed. For example, if the ORDER BY clause causes Oracle to use an index

to access the data, then Oracle may retrieve the rows in a different order than without

the index.

例如:select * from area where rownum <= 8 order by district;

其结果如下图所示:

 

发现没有,它只对area表中的前8条记录进行排序。那么,如果我要取表中的前8条记录并且要求是全表有序,那怎么办呢?还是老办法,使用子查询。我们可以使用以下语句得到:

select * from (select * from area order by district)

where rownum <= 8;

查询的结果如下图所示:

 

4 结束语

Oracle中的rownummysqllimit实现的功能相同,但没有mysql来的容易,它一般通过一个子查询来实现。mysql的易用性也是它能够纵横开源数据库的原因,它不像postgresql那样的学院派,它的那种简单易用性或许在大型软件项目的开发中值得借鉴。最近听说sql server 2008也实现了limit的查询,不过还没去试过,Oracle在这方面也要加油啊,用户容易使用才是王道。

猜你喜欢

转载自blog.csdn.net/qq_43050077/article/details/108207921