Oracle SQL 中的“限制”子句“SQL 命令未正确结束”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48780903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:30:17  来源:igfitidea点击:

'limit' clause in Oracle SQL "SQL command not properly ended"

sqloraclelimit

提问by Pratyush Panshikar

I know that questions related to 'limit' have been asked before here, and I have already referred to them. My question is somewhat different.

我知道这里之前已经问过与“限制”相关的问题,我已经提到了它们。我的问题有些不同。

Here's my query:

这是我的查询:

select id,somecol from sometable where someval=2 order by id desc limit 3

I'm getting an error saying 'SQL command not properly ended'. How do I resolve this? If you need additional information, feel free to tell me so.

我收到一条错误消息,提示“SQL 命令未正确结束”。我该如何解决?如果您需要其他信息,请随时告诉我。

回答by Jayanth

Generally, we use LIMIT in MYSQL database and Rownum in Oracle.

一般我们在MYSQL数据库中使用LIMIT,在Oracle中使用Rownum。

MySQL Syntax:

MySQL 语法

SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

SELECT column_name(s) FROM table_name WHERE condition LIMIT number;

Oracle Syntax:

甲骨文语法

SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;

SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;

References:

参考:

https://www.w3schools.com/sql/sql_top.asp

https://www.w3schools.com/sql/sql_top.asp

回答by Kaushik Nayak

If you are running Oracle 12c, you could use FETCH FIRST n ROWS ONLY:

如果您运行的是 Oracle 12c,则可以使用FETCH FIRST n ROWS ONLY

SELECT id, somecol
       FROM sometable
      WHERE someval = 2
   ORDER BY id DESC
FETCH FIRST 3 ROWS ONLY;