在mysql查询中使用偏移和限制添加顺序

时间:2021-09-06 04:06:59

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对其结果进行排序,因此结果将首先填充为最后添加的数据。

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.

提前致谢。

2 个解决方案

#1


31  

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之后。

#2


3  

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偏移量,row_ count语法或LIMIT row_count OFFSET偏移量。

Check: http://dev.mysql.com/doc/refman/5.0/en/select.html

检查:http://dev.mysql.com/doc/refman/5.0/en/select.html

#1


31  

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之后。

#2


3  

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偏移量,row_ count语法或LIMIT row_count OFFSET偏移量。

Check: http://dev.mysql.com/doc/refman/5.0/en/select.html

检查:http://dev.mysql.com/doc/refman/5.0/en/select.html