Negative limit offset in mysql

喜你入骨 提交于 2019-12-05 20:06:18

You can either do a real pain in the butt single select query, or just do this:

(SELECT * FROM highscores 
WHERE score <= ( SELECT score FROM highscores WHERE userID = someID ) 
ORDER BY score, updated ASC 
LIMIT 9)
UNION
(SELECT * FROM highscores 
WHERE score = ( SELECT score FROM highscores WHERE userID = someID ))
UNION 
(SELECT * FROM highscores 
WHERE score >= ( SELECT score FROM highscores WHERE userID = someID ) 
ORDER BY score, updated ASC
LIMIT 9)

I threw in a piece to grab the indicated user's score so it's in the middle of the list. Optional if you need it. Also, don't use SELECT *, use specific fields. Clarity is always preferable, and performance wise, * sucks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!