MySQL从排行榜上获得排名 [英] MySQL Get Rank from Leaderboards

查看:196
本文介绍了MySQL从排行榜上获得排名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于非常好的教程在游戏中实现排行榜.

I am implementing leaderboards in a game based on very good tutorial.

我对MySQL还是很陌生,但是我掌握了一些基础知识.但是代码的一部分我完全不知道它是如何工作的,并且因为我不想实现对我来说没有任何意义的东西,所以我想问问是否有人可以请我帮助理解这一点.它可以处理排行榜中返回玩家的排名:

I am quite new to MySQL, but I got some basics. But there is a part of the code which I am totally clueless how actually works and because I don't want to implement something which doesn't make any sense to me I wanted to ask if someone could please help me understand this. It handles returning player's rank in the leaderboards:

SELECT  uo.*,
    (
    SELECT  COUNT(*)
    FROM    Scores ui
    WHERE   (ui.score, -ui.ts) >= (uo.score, -uo.ts)
    ) AS rank
FROM    Scores uo
WHERE   name = '$name';

我的理解是,首先选择了分数"表中的所有内容,然后选择了行数,但我不了解该选择的工作原理,加上时间戳,WHERE的工作方式完全超出了我的范围.然后我不确定它是如何一起工作的.

My understanding is that first everything in the Scores table gets selected, then amount of rows gets selected, I don't understand how that selection works tho, how is the WHERE working is totaly beyond me with the timestamp. And then I am not sure how it works all together.

推荐答案

数据库执行此查询时,首先从Scores中选择,并按name = '$name'进行过滤.

When the database executes this query, first it selects from Scores, filtering by name = '$name'.

然后,对于每一行,它将执行子查询:

Then, for every row, it executes the subquery:

(
 SELECT  COUNT(*)
   FROM    Scores ui
  WHERE   (ui.score, -ui.ts) >= (uo.score, -uo.ts)
) AS rank

这意味着,对于具有搜索名称的Scores的每一行,它将搜索Scores中的记录,其中(ui.score, -ui.ts)大于或等于当前行的值.

It means that, for every row of Scoreswith the searched name, it searches how many records are in Scores where (ui.score, -ui.ts) is greater or equals to the current row's values.

我希望能对您有所帮助...

I hope to have helped you...

这篇关于MySQL从排行榜上获得排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆