MySQL 在mysql查询中添加带有偏移量和限制的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8149175/
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
Adding order by with offset and limit in mysql query
提问by Bala
I have a mysql query
我有一个 mysql 查询
SELECT * FROM lead LIMIT 5 OFFSET 0
to select data from the table lead and limit the results to 5 with offset of 0. I would like to order the results by its id by desc, so the results will be populated as the last added data first.
从表中选择数据,并将结果限制为 5,偏移量为 0。我想按 desc 的 id 对结果进行排序,因此结果将首先填充为最后添加的数据。
I tried
我试过
SELECT * FROM lead LIMIT 5 OFFSET 0 order by id desc
but its not working...Please correct me where am wrong and what to do.
但它不起作用......请纠正我哪里错了,该怎么做。
Thanks in advance.
提前致谢。
回答by Dennis
You have to
你必须
select * from lead order by id desc LIMIT 5 OFFSET 0
The manual ( http://dev.mysql.com/doc/refman/5.0/en/select.html) describes that LIMIT is only allowed to appear after the ORDER BY.
手册(http://dev.mysql.com/doc/refman/5.0/en/select.html)描述了 LIMIT 只允许出现在 ORDER BY 之后。
回答by CodeZombie
The ORDER BY
clause come before the LIMIT
clause. This makes sense because you first want the record set to be ordered and then apply the limitation.
该ORDER BY
子句来之前LIMIT
条款。这是有道理的,因为您首先希望对记录集进行排序,然后应用限制。
SELECT * FROM lead ORDER BY id DESC LIMIT 0, 5
You can use either LIMIT offset, row_ count
syntax or the LIMIT row_count OFFSET offset
.
您可以使用LIMIT offset, row_ count
语法或LIMIT row_count OFFSET offset
.