mysql - 偏移量问题

发布于 2024-09-05 15:59:32 字数 393 浏览 11 评论 0原文

我最近发布了一个关于以正确的顺序获取表中最后 3 个结果的问题。我现在希望以正确的顺序获取除最后 3 条之外的所有评论。

这是我的语法;

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      OFFSET 3) AS T 
ORDER BY TIME_STAMP

我收到的错误是:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行“OFFSET, 3) AS T ORDER BY TIME_STAMP”附近使用的正确语法

我似乎无法让它工作。非常感谢任何帮助。

I recently posted a question about getting last 3 results in table in the correct order. I now want the get all comments apart from the last 3 in the correct order.

Here is my syntax;

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      OFFSET 3) AS T 
ORDER BY TIME_STAMP

The error I am receiving is:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OFFSET, 3) AS T ORDER BY TIME_STAMP' at line 1

I can't seem to get it to work. Any help much appreciated.

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

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

发布评论

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

评论(3

七婞 2024-09-12 15:59:32

根据 MySQL 文档

检索某个特定行的所有行
偏移到结果末尾
设置,您可以使用一些大数字
第二个参数。此声明
检索第 96 行中的所有行
到最后:

他们建议您使用如下查询:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

因此,在您的情况下,您应该尝试:

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      LIMIT 3,18446744073709551615) AS T 
ORDER BY TIME_STAMP

请注意,您还可以使用关键字 OFFSET 使用 PostgreSQL 兼容版本:

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      LIMIT 18446744073709551615 OFFSET 3) AS T 
ORDER BY TIME_STAMP

以防万一您想知道,18446744073709551615 = 2^64 - 1

According to the MySQL Documentation:

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:

They recommend you use a query such as:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

So in your case, you should try:

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      LIMIT 3,18446744073709551615) AS T 
ORDER BY TIME_STAMP

Note that you can also use the PostgreSQL compatible version using the keyword OFFSET:

SELECT *
FROM (SELECT * 
      FROM $table 
      ORDER BY ID DESC 
      LIMIT 18446744073709551615 OFFSET 3) AS T 
ORDER BY TIME_STAMP

Just in case you are wondering, 18446744073709551615 = 2^64 - 1.

恍梦境° 2024-09-12 15:59:32

没有 LIMIT 就不能使用 OFFSET。

有点庞大,但该查询对我有用,并且如果没有冗余的内部子查询(mysql 5.0.90)就无法工作

select * from $table 
where id not in (
  select id from (
    select id from languages order by id DESC LIMIT 3
  ) l1
) order by time_stamp

You can't use OFFSET without a LIMIT.

A little bulky, but that query worked for me, and not worked without an redundant internal subquery (mysql 5.0.90)

select * from $table 
where id not in (
  select id from (
    select id from languages order by id DESC LIMIT 3
  ) l1
) order by time_stamp
Oo萌小芽oO 2024-09-12 15:59:32

遇到同样的问题,因为我复制了:

OFFSET :offset LIMIT :limit

来自现有查询,该查询在 Postgres 中有效。

显然,MySQL 中的顺序很重要,因此颠倒顺序对我有用:

LIMIT :limit OFFSET :offset 

Was having the same issue because I copied:

OFFSET :offset LIMIT :limit

from an existing query, which is valid in Postgres.

Apparently the order matters in MySQL, so reversing the order worked for me:

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