SQL SQL中Top和Limit关键字的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5668540/
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
Difference between Top and Limit Keyword in SQL
提问by reggie
A quick Question. Suppose I have the following two queries:
一个快速的问题。假设我有以下两个查询:
SELECT TOP 2 * FROM Persons;
and
和
SELECT * FROM Persons limit 2;
I want to know the difference between the execution of the above 2 queries?
Basically, I want to know when should I use the limit
keyword and when it is appropriate to use the top
keyword.
Also, How does the database return results based on the above 2 queries.
我想知道上面2个查询的执行之间的区别?基本上,我想知道什么时候应该使用limit
关键字以及什么时候使用关键字比较合适top
。另外,数据库如何根据上述2个查询返回结果。
回答by Martin Smith
If you are using SQL Server use TOP
if you are using MySQL
or Postgres
use Limit
!
如果您使用的是 SQL Server,请使用TOP
if you are usingMySQL
或Postgres
use Limit
!
AFAIK there is no product that currently supports both. Here's onelist of current implementations and here's another(covers more products but in less detail)
回答by rene
As stated in my comment for Martin Smith's answer above, there are products that support both, LIMIT
and TOP
(as you can see here). The difference is that TOP
only selects the first n records, but LIMIT
allows the definition of an offset to retrieve a specific range of records:
正如我在上面马丁·史密斯的回答评论说,有支持的产品,LIMIT
以及TOP
(因为你可以看到这里)。不同之处在于TOP
只选择前 n 条记录,但LIMIT
允许定义偏移量来检索特定范围的记录:
SELECT * FROM ... LIMIT 5 OFFSET 10
This statement selects the first 5 records, after skipping 10 records and this isn't possible with TOP
.
此语句在跳过 10 条记录后选择前 5 条记录,而TOP
.
The example I posted is only checked against the DBS I linked above. I didn't check a SQL standard, because of a lack of time.
我发布的示例仅针对我上面链接的 DBS 进行了检查。由于时间不够,我没有检查 SQL 标准。
回答by Chris Harrod
TOP & LIMIT both work on amazon Redshift
TOP & LIMIT 都在亚马逊 Redshift 上工作
回答by Andomar
limit
works on MySQL and PostgreSQL, top
works on SQL Server, rownum
works on Oracle.
limit
适用于 MySQL 和 PostgreSQL,top
适用于 SQL Server,rownum
适用于 Oracle。
回答by Fire Knight
There is no difference. The TOP
and LIMIT
keywords function identically, and will return the same thing.
没有区别。在TOP
与LIMIT
关键字相同的功能,并且将返回相同的事情。
回答by Talpa
one big mistake, LIMIT is slowly because select is return full and then database server return only limited data. When it is posible used to TOP.
一个大错误,LIMIT 很慢,因为选择返回完整,然后数据库服务器只返回有限的数据。当它可能用于TOP。