为什么我们不能在Oracle中使用条件rownum = 5 [英] why we cannot use condition rownum=5 in oracle

查看:379
本文介绍了为什么我们不能在Oracle中使用条件rownum = 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观察到rownum可以按如下方式使用:

I observed that rownum can be used as follows:

1)  select * from emp where rownum<5;
2)  select * from emp where rownum<=5;
3)  select * from emp where rownum=1;

所有这些查询均返回预期的输出.假设表emp有7行,则第一查询返回4行,第二查询返回5行,第三查询返回1行. 但是当我尝试使用类似的东西时:

All this queries return expected output. Say table emp has 7 rows then 1st query returns 4 rows, 2nd returns 5 rows ,3rd returns 1 row. But when I try to use something like:

4)  select * from emp where rownum=5;
5)  select * from emp where rownum>5;
6)  select * from emp where rownum>=5;
7)  select * from emp where rownum between 5 and 10;

在所有这些情况下,它返回0行.为什么会这样呢?有什么具体原因吗?

In all this cases, it returns 0 rows. Why this is so? Is there any specific reason for this?

推荐答案

您可以执行类似的操作:

You can do something similar like this:

select * from (
  select
    emp.*,
    row_number() over (order by fieldname) as rnum
  from emp
) tmp
where
  rnum between 5 and 10;

这篇关于为什么我们不能在Oracle中使用条件rownum = 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆