mysql 取排名 rank,MySQL从排行榜上获得排名

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

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';

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.

解决方案

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

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值