作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从表中返回 7 个事件(从今天开始),并按日期顺序排列它们:
SELECT ID
FROM table
where ID in (select ID from table
where DATEFIELD >= trunc(sysdate)
order by DATEFIELD ASC)
and rownum <= 7
如果我删除“order by”,它会很好地返回 ID,并且查询可以工作,但顺序不正确。我将不胜感激任何帮助,因为我似乎无法弄清楚我做错了什么!
(编辑)澄清一下,我之前用过这个,返回的订单确实出问题了:
select ID
from TABLE
where DATEFIELD >= trunc(sysdate)
and rownum <= 7
order by DATEFIELD
谢谢
最佳答案
在处理 ORDER BY 之前应用ROWNUM
“函数”的值。这就是为什么它不能按照您使用的方式工作( See the manual 类似的解释)
当使用ROWNUM
限制查询并涉及ORDER BY时,排序必须在内部选择中完成,并且限制必须在外部选择中应用:
select *
from (
select *
from table
where datefield >= trunc(sysdate)
order by datefield ASC
)
where rownum <= 7
关于sql - Order by 不适用于 Oracle 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11882214/
我是一名优秀的程序员,十分优秀!