0

我有一个关于 mysql 限制的问题,我使用查询

select id,name from tbl where id='2' order by name limit 1;

select id,name from tbl where id='2' order by name limit 1,2;

其中第一个查询查询显示该行,但第二个不显示。如果我使用“limit 1,2”而不是“limit 1”,如何编写有效的查询。我将它与 ajax 一起使用,以进行分页。

谢谢

4

2 回答 2

3

的语法LIMIT

LIMIT {[offset,] row_count | row_count OFFSET offset}

(见文档

您添加了 的偏移量1,因此它将跳过第一个(也是唯一一个)命中。因此您不会得到任何返回的记录!

而是尝试使用

LIMIT 0,2

(尽管有(希望如此!)一张记录使用id='2'它没有多大意义LIMIT

于 2013-01-26T12:10:33.453 回答
2

你有 id=2 这会给你一个记录。

如果您有更多 id=2 的 id,那么您可以使用 LIMIT 1,2

这是一个限制的例子

      SELECT * FROM `your_table` LIMIT 0, 10 

    //This will display the first 10 results from the database.

      SELECT * FROM `your_table` LIMIT 5, 5 

      //This will show records 6, 7, 8, 9, and 10

在你的情况下

     limit 1   <---- will show the first record in your table
     limit 1,2 <----will show records first and second  in your table

如果你的 id 是自动增量的,你不需要在这里限制,因为它已经是唯一的

于 2013-01-26T12:15:39.483 回答