MySQL-按字段排序,从特定值开始

时间:2019-10-16 16:51:20

标签: mysql sql-order-by

我希望这是可能的。

我有这张桌子:

enter image description here

我想每行解析5行并按分数排序,所以当我这样做时:

SELECT * FROM test ORDER BY score ASC LIMIT 5

结果是: enter image description here

现在可以说有人用id 11 and score 2添加了一个新商品,然后我做了:

SELECT * FROM test ORDER BY score ASC LIMIT 5, 5

要获取剩余的行,那么我会再次收到id 7

如何防止这种情况?因此,在id 7

之后获取其余项

1 个答案:

答案 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;

其中lastScoreDisplayedlastIdDisplayed是上一个查询的最后一行的值。

这种技术 甚至比LIMIT x, x技术还快; LIMIT x, x每次都必须对整个数据集进行排序,而随着时间的推移,这项技术最终会产生一个较小的集合。

相关问题