从工资中选择 rownum,其中 rownum=3;

发布于 2024-09-14 01:05:43 字数 49 浏览 4 评论 0原文

如何使用“rownum”关键字从任何表中检索第三行(我使用的是oracle-10g)

How to retrieve third row from any table using "rownum" key word ( i am using oracle-10g)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜司空 2024-09-21 01:05:43

当查询生成行时,Oracle 按顺序将值分配给 ROWNUM - 因此,获取的第一行获取 ROWNUM=1,获取的第二行获取 ROWNUM=2,获取的第三行获取 ROWNUM=3,等等。 注意 - 对于一行要分配 ROWNUM=3,必须获取前面的两行。这就是您的查询不返回任何行的原因。您向数据库询问已获取的第三行 - 但从未获取过第 1 行和第 2 行。

要进行演示,请尝试运行以下查询:

SELECT S.* FROM SALARY S;          -- Should return all rows
SELECT ROWNUM, S.* FROM SALARY S;  -- Should return all rows with ROWNUM prepended
SELECT ROWNUM, S.* FROM SALARY WHERE ROWNUM=3;  -- Should return no rows

要解决您的问题,请尝试以下操作:

SELECT ROW_NUMBER FROM
  (SELECT ROWNUM AS ROW_NUMBER, S.* FROM SALARY S)
  WHERE ROW_NUMBER = 3;

分享并享受。

Oracle assigns values to ROWNUM sequentially as rows are produced by the query - thus, the first row fetched gets ROWNUM=1, the second row fetched gets ROWNUM=2, the third row fetched gets ROWNUM=3, etc. Notice - for a row to be assigned ROWNUM=3 two preceding rows MUST be fetched. And this is why your query returns no rows. You're asking the database for the third row fetched - but rows 1 and 2 have never been fetched.

To demonstrate, try running the following queries:

SELECT S.* FROM SALARY S;          -- Should return all rows
SELECT ROWNUM, S.* FROM SALARY S;  -- Should return all rows with ROWNUM prepended
SELECT ROWNUM, S.* FROM SALARY WHERE ROWNUM=3;  -- Should return no rows

To work around your problem, try the following:

SELECT ROW_NUMBER FROM
  (SELECT ROWNUM AS ROW_NUMBER, S.* FROM SALARY S)
  WHERE ROW_NUMBER = 3;

Share and enjoy.

甜尕妞 2024-09-21 01:05:43

需要执行类似的操作

select rnum,sal
  from  
( select sal, rownum rnum

    from salary 
   order by sal desc ) 
 where rnum = 3;

rownum 直到谓词阶段之后才被分配,因此 rownum = 3 将始终为 false。使用 CTE 或派生表,然后您可以从其外部访问 rownum

You would need to do something like this

select rnum,sal
  from  
( select sal, rownum rnum

    from salary 
   order by sal desc ) 
 where rnum = 3;

rownum is not assigned until after the predicate phase so rownum = 3 will always be false. Use a CTE or derived table then you can access the rownum from outside it.

遗失的美好 2024-09-21 01:05:43
SELECT ROW_NUMBER FROM   (SELECT ROWNUM AS ROW_NUMBER, S.* FROM SALARY S)   WHERE ROW_NUMBER = 3; 

这是不正确的。这将始终返回“3”,因为您选择的是 ROW_NUMBER。

它应该是“select *”,如下所述:

select * from (select rownum as row_number, s.* from salary s) where row_number = 3;
SELECT ROW_NUMBER FROM   (SELECT ROWNUM AS ROW_NUMBER, S.* FROM SALARY S)   WHERE ROW_NUMBER = 3; 

This is incorrect. This will always return '3', because you are selecting ROW_NUMBER.

It should instead be "select *", as mentioned below:

select * from (select rownum as row_number, s.* from salary s) where row_number = 3;
荒芜了季节 2024-09-21 01:05:43

rownum 是薪资表中的实际列吗?如果不是,根据您的数据库类型,rownum 可能不受支持。

Is rownum an actual column in your salary table? If not, depending on your DB type, rownum is likely not supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文