如何限制 SQL 查询的结果

发布于 2024-09-07 09:11:51 字数 124 浏览 3 评论 0原文

我想知道是否可以限制 SQL 请求的结果?

例如,仅返回最多 50 行:

  SELECT * FROM <table>

thanks。

I'm wondering is it possible to limit the result of a SQL request?

For example, only return up to 50 rows from:

  SELECT * FROM <table>

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

屋檐 2024-09-14 09:12:27

在 MySQL 中,您将使用 LIMIT 子句

 SELECT * FROM <table> LIMIT 50

In MySQL you would use a LIMIT clause

 SELECT * FROM <table> LIMIT 50
一向肩并 2024-09-14 09:12:24

是的,在 MYSQL 中:

LIMIT 子句可用于限制 SELECT 语句返回的行数。 LIMIT 接受一个或两个数字参数,它们必须都是非负整数常量(使用准备好的语句时除外)。

使用两个参数,第一个参数指定要返回的第一行的偏移量,第二个参数指定要返回的最大行数。初始行的偏移量为 0(不是 1):

SELECT * FROM tbl LIMIT 5,10; # 检索行 6-15

要检索从某个偏移量到结果集末尾的所有行,可以为第二个参数使用一些较大的数字。此语句检索从第 96 行到最后一行的所有行:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

使用一个参数,该值指定从结果集开头返回的行数:

从 tbl LIMIT 5 中选择*; # 检索前 5 行

换句话说,LIMIT row_count 相当于 LIMIT 0, row_count。

Yes is possible, in MYSQL:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be non-negative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

傲影 2024-09-14 09:12:22

您可以使用 TOP 子句

SELECT TOP 50 * FROM <table>

如果您的数据库不支持它,您也可以尝试LIMITROWNUM 但这又取决于您正在使用的数据库。

You could use the TOP clause:

SELECT TOP 50 * FROM <table>

If your database doesn't support it you may try also LIMIT and ROWNUM but once again this will depend on the database you are using.

蝶舞 2024-09-14 09:12:20

是的,这是可能的。这在数据库引擎之间有所不同。

Postgres:

SELECT * FROM <table> LIMIT 50

SQL Server:

SELECT TOP 50 * FROM <table> 

Yes, this is possible. This differs between db engines.

Postgres:

SELECT * FROM <table> LIMIT 50

SQL Server:

SELECT TOP 50 * FROM <table> 
于我来说 2024-09-14 09:12:16

SQL 标准

SQL:2008 标准提供以下语法来限制 SQL 结果集:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
FETCH FIRST 50 ROWS ONLY

Oracle 从 12c 开始支持 SQL:2008 Top-N 记录子句,SQL Server 从 2012 年开始支持,PostgreSQL 从 8.4 开始支持。

SQL Server

虽然 SQL Server 支持 SQL:2008 Top-N 标准语法,您还需要提供 OFFSET 子句:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
OFFSET 0 ROWS
FETCH FIRST 50 ROWS ONLY

在较旧的 SQL Server 版本上,您可以使用 TOP:

SELECT TOP 50
    title
FROM
    post
ORDER BY
    id DESC

Oracle 11g 和较早版本

在版本 12c 之前,要获取 Top-N 记录,您必须使用派生表和ROWNUM 伪列:

SELECT *
FROM (
    SELECT
        title
    FROM
        post
    ORDER BY
        id DESC
)
WHERE ROWNUM <= 50

MySQL 和 PostgreSQL 8.3 或更早版本

传统上,MySQL 和 PostgreSQL 使用 LIMIT 子句将结果集限制为 Top-N 记录:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
LIMIT 50

SQL Standard

The SQL:2008 Standard provides the following syntax to limit the SQL result set:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
FETCH FIRST 50 ROWS ONLY

The SQL:2008 Top-N records clause is supported in Oracle since 12c, SQL Server since 2012, and PostgreSQL since 8.4.

SQL Server

While SQL Server supports the SQL:2008 Top-N standard syntax, you need to provide the OFFSET clause as well:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
OFFSET 0 ROWS
FETCH FIRST 50 ROWS ONLY

On older SQL Server versions, you can use TOP:

SELECT TOP 50
    title
FROM
    post
ORDER BY
    id DESC

Oracle 11g and older versions

Prior to version 12c, to fetch the Top-N records, you had to use a derived table and the ROWNUM pseudocolumn:

SELECT *
FROM (
    SELECT
        title
    FROM
        post
    ORDER BY
        id DESC
)
WHERE ROWNUM <= 50

MySQL and PostgreSQL 8.3 or older

Traditionally, MySQL and PostgreSQL use the LIMIT clause to restrict the result set to the Top-N records:

SELECT
    title
FROM
    post
ORDER BY
    id DESC
LIMIT 50
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文