MySQL: select 5 rows before and after specific row

后端 未结 5 513
陌清茗
陌清茗 2020-12-20 23:26

I have table called \"users\" and need to select 2 rows before and after specific row, sorted by users.score ASC

users table (structure):

id     name         


        
5条回答
  •  旧时难觅i
    2020-12-21 00:09

    (SELECT x.* FROM users x JOIN users y ON y.score <= x. score WHERE y.id = 5 ORDER BY score LIMIT 3)
    UNION
    (SELECT x.* FROM users x JOIN users y ON y.score >= x. score WHERE y.id = 5 ORDER BY score DESc LIMIT 3)
    [ORDER BY score]    ;
    

    http://www.sqlfiddle.com/#!9/45c22/42

提交回复
热议问题