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

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

Difference between Top and Limit Keyword in SQL

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 limitkeyword and when it is appropriate to use the topkeyword. 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 TOPif you are using MySQLor Postgresuse Limit!

如果您使用的是 SQL Server,请使用TOPif you are usingMySQLPostgresuse 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)

AFAIK 目前没有产品同时支持两者。是当前实现的一个列表,这是另一个(涵盖更多产品但不太详细)

回答by rene

As stated in my comment for Martin Smith's answer above, there are products that support both, LIMITand TOP(as you can see here). The difference is that TOPonly selects the first n records, but LIMITallows 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

limitworks on MySQL and PostgreSQL, topworks on SQL Server, rownumworks on Oracle.

limit适用于 MySQL 和 PostgreSQL,top适用于 SQL Server,rownum适用于 Oracle。

回答by Fire Knight

There is no difference. The TOPand LIMITkeywords function identically, and will return the same thing.

没有区别。在TOPLIMIT关键字相同的功能,并且将返回相同的事情。

回答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。