mysql - 偏移量问题
我最近发布了一个关于以正确的顺序获取表中最后 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 MySQL 文档:
他们建议您使用如下查询:
因此,在您的情况下,您应该尝试:
请注意,您还可以使用关键字
OFFSET
使用 PostgreSQL 兼容版本:以防万一您想知道,
18446744073709551615 = 2^64 - 1
。According to the MySQL Documentation:
They recommend you use a query such as:
So in your case, you should try:
Note that you can also use the PostgreSQL compatible version using the keyword
OFFSET
:Just in case you are wondering,
18446744073709551615 = 2^64 - 1
.没有 LIMIT 就不能使用 OFFSET。
有点庞大,但该查询对我有用,并且如果没有冗余的内部子查询(mysql 5.0.90)就无法工作
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)
遇到同样的问题,因为我复制了:
来自现有查询,该查询在 Postgres 中有效。
显然,MySQL 中的顺序很重要,因此颠倒顺序对我有用:
Was having the same issue because I copied:
from an existing query, which is valid in Postgres.
Apparently the order matters in MySQL, so reversing the order worked for me: