Oracle 中取前几条记录(分页)

Oracle中用于类似MSSQL中top的关键字为rownumber,具体用法如下:
select firmcode,balance from (
                 select   rownum   rn,t.firmcode,t.balance
                 from FIRMBALANCE_TAB t
                 order by balance desc
          ) tab
          where tab.rn >0 and tab.rn < 11;


注:tab指的是: select   rownum   rn,t.firmcode,t.balance   from FIRMBALANCE_TAB t        order by balance desc
查询得到的结果集.
tab.rn 指的是:select rownum rn
rownum是oracle给出的一个用来表明当前记录位置的一个字段 

下面的也是正确的:
select firmcode from FirmTable where rownum < 10 取前十条记录;
select firmcode from firmTable where rownum = 1 取第一条记录
但是下面是不正确的:
select firmcode from firmTable where rownum = 2 ,因为rownum都是从1开始的,没第一条不可能出来第二条的
另外,按oracle 9i参考手册所言,如果跟order by的话需要用嵌套查询:
If you embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query, then you can force the ROWNUM condition to be applied after the ordering of the rows. For example, the following query returns the 10 smallest employee numbers. This is sometimes referred to as a "top-N query":
因为rownum是已经排序好的结果集中的行号,并且用在最上层的限制中.
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值