我有一个高分数据库,可以跟踪各种世界中的每个游戏。我想要做的是找出有关戏剧的一些统计数据,然后找出每个世界“排名”的位置。根据彼此的世界(按播放次数排序)。

到目前为止,我的所有统计数据都运行正常,但是我在找到每个世界的排名时遇到了问题。

我也很确定在三个单独的查询中执行此操作可能是一种非常缓慢的方法,可能会有所改进。

我有一个时间戳列(此处未使用)和“世界”列。数据库架构中索引的列。以下是我的选择:

function getStast($worldName) {
  // ## First find the number of wins and some other data:
  $query = "SELECT COUNT(*) AS total, 
            AVG(score) AS avgScore, 
            SUM(score) AS totalScore
            FROM highscores 
            WHERE world = '$worldName'
            AND victory = 1";
  $win = $row['total'];

  // ## Then find the number of losses:
  $query = "SELECT COUNT(*) AS total 
            FROM highscores 
            WHERE world = '$worldName'
            AND victory = 0";
  $loss = $row['total'];

  $total = $win + $loss;

  // ## Then find the rank (this is the broken bit):
  $query="SELECT world, count(*) AS total 
          FROM highscores 
          WHERE total > $total
          GROUP BY world
          ORDER BY total DESC";

  $rank = $row['total']+1;
  // ## ... Then output things.
}

我认为失败的特定代码行是在RANK查询中,

              WHERE total > $total

它不起作用,因为它不能接受计算的总数作为WHERE子句中的参数吗?

最后,是否有更有效的方法在单个SQL查询中计算所有这些?

有帮助吗?

解决方案

我想你可能想要使用'总计> $总'?

SELECT world, count(*) AS total 
FROM highscores 
GROUP BY world
having total > $total
ORDER BY total DESC
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top