oracle数据库中的查询限制

时间:2018-05-17 11:19:36

标签: sql oracle

我正在尝试使用一种限制来创建查询。

SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 AND ROWNUM<=51 ORDER BY TIMESTAMP DESC

我使用rownum但首先是限制,然后按顺序选择。但是我需要先订购再限制。我在enter link description here找到了我应该使用的FETCH FIRST 51 ROWS ONLY。不幸的是它不起作用。

SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 ORDER BY TIMESTAMP DESC FETCH FIRST 51 ROWS ONLY;

它引发了以下错误:

SQL Error [933] [42000]: ORA-00933: SQL command not properly ended

oracle.jdbc.OracleDatabaseException: ORA-00933: SQL command not properly ended

2 个答案:

答案 0 :(得分:3)

你只会在最后错过。如果我没记错的话,这个语法从12c R1开始可用。您使用的是哪个Oracle版本?

SELECT * FROM ALARMS WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171 ORDER BY TIMESTAMP DESC FETCH FIRST 51 ROWS ONLY

编辑:

由于您的版本是11g,然后尝试使用这样的语法(我希望它可以工作;))

SELECT * 
FROM (
    SELECT a.*,ROW_NUMBER() OVER(ORDER BY TIMESTAMP DESC) rcnt 
    FROM ALARMS A WHERE OBJECT_ID=0 AND TIMESTAMP<=1525677504171) src  
WHERE src.rcnt <= 51
ORDER BY src.TIMESTAMP desc;

答案 1 :(得分:2)

sendEmail({ to: "email", from: request.params.email, subject: "subject", text: "text", attachments: [{ content: b3, // 'Some base 64 encoded attachment content' filename: 'some-attachment.txt', type: 'plain/text', disposition: 'attachment', contentId: 'mytext' } ], }); 仅在Oracle 12c之后可用。

对于FETCH FIRST方法,请使用包含rownum的子查询,然后限制封闭查询中的行:

order by