MySQL获得偏移,给定限制,排序依据和位置ID

时间:2012-02-05 12:39:51

标签: mysql pagination limit offset

我有一个每页显示10行的表。现在,给定一个项目的ID,我如何获得该项目所在的“页面”?

换句话说,我正在寻找给定记录ID的偏移量(10的倍数)。

SELECT * FROM items
WHERE category = "category"
LIMIT ?, 10      <----- TRYING TO FIGURE OUT HOW TO GET ? OFFSET GIVEN ID
ORDER BY id DESC

2 个答案:

答案 0 :(得分:0)

这应该这样做;

SELECT 10*FLOOR(COUNT(id)/10) FROM items where id < YOUR_ID

答案 1 :(得分:0)

试试这个:

SET @rownum = 0; 
Select sub.*, 10 * Floor(sub.rownum / 10) as pagenum
from
(
   Select *,  (@rownum:=@rownum+1) as rownum
   from items order by id desc
) sub
where sub.ID = givenID;