根据比赛计算球员排名

时间:2011-01-17 10:25:31

标签: sql mysql

我需要显示一个图表,其中可以显示游戏,得分,所有时间排名,每周排名,与朋友排名。

我有两张桌子:
1)high_scores(playerid,gameid,得分,时间(时间戳))
2)朋友(playerid,friendid)

我想知道如何使用最少数量的查询来实现这一目标。

我正在使用PHP& MySQL用于我的游戏网站。

2 个答案:

答案 0 :(得分:1)

可以设法查询它,如下所示:

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

感谢您的回答...... -Navi

答案 1 :(得分:0)

计算一次数据,而不是每次请求。

你有2个解决方案:

  • cron / async job
  • 在每次插入/更新/删除/截断
  • 后触发重新计算统计信息