从数据库中获取玩家排名

发布于 2024-10-18 05:05:37 字数 174 浏览 2 评论 0原文

我有一个 RuneScape 私人服务器,它将玩家分数存储在数据库中。 高分加载玩家的分数并将其放入表格中。

但现在是我无法解决的更困难的部分:
我想显示玩家的排名。比如:‘攻击等级:44,排名第12’。因此它必须找到用户的排名。

我怎样才能让它发挥作用?我用谷歌搜索了两天,没有找到任何东西。

I have a RuneScape private server, which stores the player scores in a database.
The highscores load the player's scores and put them into a table.

But now comes the harder part I can't fix:
I want to display the rank of the player. Like: 'Attack level: 44, ranked 12'. So it has to find the rank the user has.

How can I get this to work? I googled for 2 days now, I did not find anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-10-25 05:05:37

我不知道是否有办法使用相同的查询来实现此目的。

您可以进行另一个查询,例如:

pos = select count(*) fromplayers where Attack > 44 + 1

此查询将返回排名高于某人的玩家数量。 “加一”部分是让排名从 1 开始(因为第一个排名不会有任何人排名在他之上)。

例如,如果表格为:

id attack
 0     35
 1     22
 2    121
 3     76

pos(3) = 1(只有玩家 2 排在上面)+ 1 = 2

I don't know if there's a way to achieve this using the same query.

You could make another query like:

pos = select count(*) from players where attack > 44 + 1

This query would return the number of players ranked above someone. The "plus one" part is to make the rank start at 1 (because the first one won't have anyone ranked above him).

For example, if the table is:

id attack
 0     35
 1     22
 2    121
 3     76

pos(3) = 1 (only player 2 is ranked above) + 1 = 2

贪恋 2024-10-25 05:05:37

您(可能)可以创建一个显示每个玩家得分的视图。沿着这些思路的一些东西可能会起作用。

create view player_scores as 
select player_id, sum(score) 
from scores
group by player_id

这将为您提供每个玩家的一行以及他们的总得分。有了这个观点,排名就很简单了。

select count(*) 
from player_scores
where sum > (select sum from player_scores where player_id = 1)

该查询将返回分数高于player_id = 1的玩家数量。

当然,如果您在运行查询之前知道玩家的分数,则可以将该分数作为参数传递。只要该列被索引,运行速度就会快得多。

You can create a view (probably) that shows every players score. Something along these lines might work.

create view player_scores as 
select player_id, sum(score) 
from scores
group by player_id

That will give you one row per player, with their total score. Having that view, the rank is simple.

select count(*) 
from player_scores
where sum > (select sum from player_scores where player_id = 1)

That query will return the number of players having a higher score than player_id = 1.

Of course, if you know your player's score before you run the query, you can pass that score as a parameter. That will run a lot faster as long as the column is indexed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文