为什么 rownum 上的两个 sql 查询给出不同的结果
>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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为第二次的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.
在第一个示例中,内联视图中的查询返回所有行,每行都有一个递增的
ROWNUM
并分配了名称r
。In your first example the query in the inline view returns all the rows, each with an incrementing
ROWNUM
assigned the namer
.