获取有序行

wljmcqd8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(273)

你好,外面的开发者们,
我想在我的程序中创建一个排名列表,并以此来获得mysql数据库的用户按他们的分数排序。我是这样做的:

select `name`,`points` from users order by `points` desc

为了获得更好的性能,我不想让所有的用户,但只有10+和10-从一个给定的用户,因为我不想显示整个排名列表中的程序。有人能帮我吗?

jvidinwx

jvidinwx1#

一种方法使用子查询和 union all :

(select name, points
 from users
 where points <= (select u2.points from users u2 where u2.name = ?)
 order by points desc
 limit 11
) union all
(select name, points
 from users
 where points > (select u2.points from users u2 where u2.name = ?)
 order by points asc
 limit 10
) ;

返回21行,包括指定的用户。

相关问题