我希望这是可能的。
我有这张桌子:
我想每行解析5行并按分数排序,所以当我这样做时:
SELECT * FROM test ORDER BY score ASC LIMIT 5
现在可以说有人用id 11 and score 2
添加了一个新商品,然后我做了:
SELECT * FROM test ORDER BY score ASC LIMIT 5, 5
要获取剩余的行,那么我会再次收到id 7
如何防止这种情况?因此,在id 7
答案 0 :(得分:0)
如果您ORDER BY score, id
,则应该可以使用过滤功能。
SELECT *
FROM test
WHERE (score = lastScoredDisplayed AND id > lastIdDisplayed) -- Continue with last score
OR score > lastScoreDisplayed -- continue on to next score
ORDER BY score, id
LIMIT 5;
其中lastScoreDisplayed
和lastIdDisplayed
是上一个查询的最后一行的值。
这种技术 甚至比LIMIT x, x
技术还快; LIMIT x, x
每次都必须对整个数据集进行排序,而随着时间的推移,这项技术最终会产生一个较小的集合。