sql - 根据玩过的游戏计算玩家排名

标签 sql mysql

我需要显示一个图表,其中可以显示游戏、分数、历史排名、每周排名、与 friend 的排名。

我有两个表:
1) high_scores (playerid, gameid, score, time(timestamp))
2) 好友(playerid, friendid)

我想知道如何使用最少数量的查询来做到这一点。

我正在为我的游戏网站使用 PHP 和 MySQL。

最佳答案

可以按如下方式对其进行查询:

SELECT
    gamescores.gameid,
    COUNT(*)+1 AS rank,
    playergamescores.total,
    gamename,
    foldername
FROM (
    SELECT gameid, playerid, SUM( score ) AS total, time
    FROM high_scores
    GROUP BY gameid, playerid
    ORDER BY gameid, total DESC
) AS gamescores
INNER JOIN
(
    SELECT gameid, SUM(score) AS total
    FROM high_scores
    WHERE playerid = 361822
    GROUP BY gameid
    ORDER BY total DESC
) AS playergamescores
ON playergamescores.gameid = gamescores.gameid
INNER JOIN gamemaster
ON gamescores.gameid = gamemaster.gameid
WHERE gamescores.total > (
    SELECT SUM( score ) AS total
    FROM high_scores
    WHERE gamescores.gameid = gameid
    AND playerid = 361822                       
)
GROUP BY gamescores.gameid
ORDER BY gamescores.time DESC

谢谢你的回答... -导航

关于sql - 根据玩过的游戏计算玩家排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4712093/

相关文章:

php - 当一个表为空时从两个表请求mysql

mysql - 如何在mysql上按年份和学期排名?

php - 我可以让 SQL 查询在每一行上进行两次匹配吗?

php - SQLSTATE[HY093] : Invalid parameter number: parameter was not defined'

MySQL如何选择每个组的第一行计数

sql - 如何将varchar列值转换为int?

用于查找每个用户的计数的 SQL

基于 SQL 集的范围

sql - 如何在 Rails 中查询这个?

mysql - 单独序列表中的 JPA 主键