Oracle 中rownum的用法

本文介绍在 Oracle 数据库中如何通过 SQL 语句选取特定行或区间内的记录,包括获取单行记录、多行记录、指定区间的记录等,并展示了如何实现基本的分页功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

选择表中的某一行记录:(理解:rownum是oracle系统顺序分配为从查询返回的行的编号)

select * from (select rownum a,t.* from testtab t) where a=2;
select * from (select rownum a,t.* from testtab t) where a=3;
select * from (select rownum a,t.* from testtab t) where a=4;


返回多行记录:
select * from testtab where rownum<=10;


返回某段记录:(如取记录表中4-10行)
select * from (select rownum no,testtab.* from testtab where rownum<=10) where no>=4;


返回有条件且经过排序的某段记录:
select rownum,tt.* from 
(select rownum num,t.* from
(select EcodeInfo.* from EcodeInfo where a=1 order by ecode desc) t
) tt
where num>19 and rownum<20


返回最后一行记录:
select * from (select rownum a,t.* from testtab t) where a=(select count(*) from testtab);


返回最后第N行记录:
select * from (select rownum a,t.* from testtab t) where a=(select count(*)-N from testtab); 


分页
select tt.num,tt.* from 
(select t.*,rownum num from (select * from infotable a where a = 10 order by a.id) t where rownum <= 20) tt where tt.num>10


不能使用
select a.* from table a where [color=red]a.rownum <10[/color];
要直接使用
select a.* from table a where [color=red]rownum <10[/color];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值