【问题标题】:Sum of statistics for every player (goals/assists/cards etc.)每个球员的统计数据总和(进球/助攻/卡等)
【发布时间】:2012-05-20 18:45:38
【问题描述】:

我正在尝试创建一个显示每个玩家统计数据的页面。总进球、助攻、黄牌、红牌等的总和。

详细信息存储在 matchdetails 中,您可以在下面看到:

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| player_id      | int(4)      | NO   |     | NULL    |       |
| match_id       | int(4)      | NO   |     | NULL    |       |
| season         | varchar(45) | NO   |     | NULL    |       |
| player_present | int(4)      | NO   |     | NULL    |       |
| player_away    | int(4)      | NO   |     | NULL    |       |
| goals          | int(4)      | NO   |     | NULL    |       |
| assists        | int(4)      | NO   |     | NULL    |       |
| yellowcards    | int(4)      | NO   |     | NULL    |       |
| redcards       | int(4)      | NO   |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

+-----------+----------+-----------+----------------+-------------+-------+---------+-------------+----------+
| player_id | match_id | season    | player_present | player_away | goals | assists | yellowcards | redcards |
+-----------+----------+-----------+----------------+-------------+-------+---------+-------------+----------+
| 3         | 1        | 2011-2012 | 1              | 0           | 0     | 0       | 0           | 0        |
| 4         | 2        | 2011-2012 | 1              | 0           | 0     | 2       | 1           | 0        |
| 4         | 1        | 2011-2012 | 1              | 0           | 0     | 0       | 0           | 0        |
| 1         | 2        | 2011-2012 | 1              | 0           | 4     | 0       | 0           | 0        |
| 1         | 1        | 2011-2012 | 0              | 1           | 0     | 0       | 0           | 0        |
| 2         | 1        | 2011-2012 | 1              | 0           | 2     | 0       | 1           | 0        |
| 2         | 2        | 2011-2012 | 1              | 0           | 1     | 2       | 0           | 1        |
+-----------+----------+-----------+----------------+-------------+-------+---------+-------------+----------+

我想要实现的目标是为每个参加过他的总比赛的球员(player_present)查看一行 ,总比赛客场(player_away),总进球数,总助攻数,总黄牌数和总红牌数正在显示。

我使用的以下代码已经显示了总进球数,但它汇总了所有球员的进球数。我希望它为每个玩家显示每个细节的总和。

$sql = "SELECT SUM(goals) AS goals, 
player_id
FROM matchdetails
ORDER BY player_id ASC
"; 
$results = $db->query($sql); 

echo "<table border='0' id='stats' cellpadding='0' cellspacing ='0'>
foreach ($results as $row) {    
    echo "<td class=''>" , $row['player_id'] , ' ' , "</td>"; 
    echo "<td class=''>" , $row['goals'] , "</td>";
}
    echo "</tr>";
echo "</table>";

【问题讨论】:

    标签: php sql pdo statistics sum


    【解决方案1】:

    添加GROUP BY 子句:

    SELECT SUM(goals) AS goals, player_id
    FROM matchdetails
    GROUP BY player_id
    ORDER BY player_id ASC
    

    【讨论】:

      【解决方案2】:

      如果你想使用SUM,你需要使用GROUP BY

      SELECT SUM(goals) AS goals, player_id
      FROM matchdetails
      GROUP BY player_id
      ORDER BY player_id ASC
      

      另外,不确定是否复制/粘贴错误,您的代码中缺少结束引号:

      $sql = "SELECT SUM(goals) AS goals, 
      player_id
      FROM matchdetails
      ORDER BY player_id ASC
      "; 
      $results = $db->query($sql); 
      
      echo "<table border='0' id='stats' cellpadding='0' cellspacing ='0'>";
      foreach ($results as $row) {    
          echo "<td class=''>" , $row['player_id'] , ' ' , "</td>"; 
          echo "<td class=''>" , $row['goals'] , "</td>";
      }
          echo "</tr>";
      echo "</table>";
      

      【讨论】:

      • 谢谢,成功了。缺少的结束引号确实是复制/粘贴错误。
      • @Joost:没问题。如果我的答案是正确的,您应该通过单击此答案的投票计数下方的绿色大勾号来接受它是正确的。只有当它确实是正确答案时才接受它!
      • 我仍然需要等待几分钟才能接受它:p
      猜你喜欢
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多