Oracle之分页

Oracle之分页,第1张

在Oracle中有一个方法rownum用来查询第一行到第n行的内容,但没有一个合适的方法若查询第x行到第y行的内容,而在实际应用中却经常需要查询第x行到第y行的内容,这时我们就需要使用rownum和子表查询等内容来进行查询,因为这一块内容属于Oracle总的常用部分所以专门在此介绍。

在Oralce中有一个伪列rownum,其在创建表的时候就存在了却不显示,若要使用这个列可以直接调用即可,也可以对这个列添加别名来调用。

rownum只能用于显示小于某行的数据即第一行开始到你要查询到的那一行为止的数据。

在Oracle把查询第几行到第几行的 *** 作称为分页,其具体 *** 作是通过子查询等 *** 作完成。

select 列名 from (select 表名.*,rownum rn from 表名)表名  where rn *** 作

思考如下:

1.选择所有内容

select * from emp

2.显示rownum

select e.*,rownum rn from(select * from emp)e

这一步可以精简为下面形式,但某些情况只能用上面那种

select emp.*,rownum rn from emp

3.查询

select * from(select e.*,rownum rn from (select * from emp)e)

4.其他变化

在某些时候我们需要先对表的内容进行排序,随后查询第x行到第y行的内容,这个时候有一个需要注意的点是rownum是在表产生的时候产生的伪列,所以使用排序会连着rownum的值进行排序,从而达不到想要的效果。

为了解决上述这个问题,我们需要使用子表查询即先排好序,再在新表之中显示rownum来规避这个问题。

考虑到排序的问题,所以在上方第二步的时候使用第一种方法即select e.*,rownum rn from(select * from emp)e,在内表select * from emp中进行排序可以完成在乱序中找到第x行到第y行的效果。

您好:oracle查询分页可分为两种情况,一种使用的是rownum ,另外一种则是使用 row_number() over(order by column_name desc)。

1.使用rownum分页查询,可用以下方式:

select t2.* from (select t1.*,rownum as rn from table_name t1 where 1=1 and rownum <= page * page_size) t2 where t2.rn >(page - 1) * page_size

2.使用 row_number() over() 分页查询

select t2.* from (select t1.*,row_number() over(order by column_name desc) as rn from table_name t1 where 1=1 )t2 where t2.rn >(page-1)* page_size and t2.rn <= page * page_size

这种方式,也是可以分页的。

希望能帮助您!

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

一种是利用相反的。

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条。

使用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

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

select * from (select rownum tid,components.* from components where rownum<=100) where tid<=10


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9809216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存