为什么 rownum 上的两个 sql 查询给出不同的结果

发布于 2024-09-15 21:41:15 字数 439 浏览 4 评论 0原文

>SELECT instmax  
FROM  
  (SELECT instmax,  
     rownum r  
  FROM  
    ( SELECT * FROM pswlinstmax ORDER BY instmax DESC NULLS LAST  
    )   
  )  
WHERE r = 2;  

INSTMAX  
-------  
1049  

>SELECT instmax  
FROM  
  (SELECT instmax,  
    rownum  
  FROM  
    (SELECT * FROM pswlinstmax ORDER BY instmax DESC  
    )  
  )  
WHERE rownum=2;  


**NO RETURNED ROW**  

为什么它给出不同的结果?我想对此进行详细的解释。

>SELECT instmax  
FROM  
  (SELECT instmax,  
     rownum r  
  FROM  
    ( SELECT * FROM pswlinstmax ORDER BY instmax DESC NULLS LAST  
    )   
  )  
WHERE r = 2;  

INSTMAX  
-------  
1049  

>SELECT instmax  
FROM  
  (SELECT instmax,  
    rownum  
  FROM  
    (SELECT * FROM pswlinstmax ORDER BY instmax DESC  
    )  
  )  
WHERE rownum=2;  


**NO RETURNED ROW**  

why it is giving different result? I would like to have detailed explanation on this.

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

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

发布评论

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

评论(2

旧人 2024-09-22 21:41:15

因为第二次的rownum不是内层SQL的rownum而是外层SQL的rownum!

这就是为什么你需要“重命名”它,以便它变得“固定”。

否则, rownum =2 过滤器永远不会匹配,因为每一行都是第一行。

because the secont time, the rownum is not the rownum of the inner SQL but the rownum of the outer one!

that's the reason why you need to "rename" it, so that it becomes "fixed".

otherwise, the rownum =2 filter never matches because every row is the first.

放赐 2024-09-22 21:41:15
SELECT instmax   
FROM   
  (SELECT instmax,   
    rownum   
  FROM   
    (SELECT * FROM pswlinstmax ORDER BY instmax DESC   
    )   
  )   
WHERE rownum=2;   


**NO RETURNED ROW**   

获取的第一行被分配了一个
ROWNUM 为 1 并满足条件
错误的。要获取的第二行是
现在第一行也被分配
ROWNUM 为 1 并满足条件
错误的。所有行随后都失败
满足条件,所以没有行
回来了。 Oracle9i SQL 参考< /p>

在第一个示例中,内联视图中的查询返回所有行,每行都有一个递增的 ROWNUM 并分配了名称 r

SELECT instmax   
FROM   
  (SELECT instmax,   
    rownum   
  FROM   
    (SELECT * FROM pswlinstmax ORDER BY instmax DESC   
    )   
  )   
WHERE rownum=2;   


**NO RETURNED ROW**   

The first row fetched is assigned a
ROWNUM of 1 and makes the condition
false. The second row to be fetched is
now the first row and is also assigned
a ROWNUM of 1 and makes the condition
false. All rows subsequently fail to
satisfy the condition, so no rows are
returned. Oracle9i SQL Reference

In your first example the query in the inline view returns all the rows, each with an incrementing ROWNUM assigned the name r.

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