0

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

详细信息存储在 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>";
4

2 回答 2

1

添加一个GROUP BY子句:

SELECT SUM(goals) AS goals, player_id
FROM matchdetails
GROUP BY player_id
ORDER BY player_id ASC
于 2012-05-12T12:33:31.940 回答
1

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

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>";
于 2012-05-12T12:39:21.370 回答