我正在尝试创建一个显示每个玩家统计信息的页面。总目标,助攻,黄牌,红牌等总和。
详细信息存储在您可以在下面看到的匹配详细信息中:
+----------------+-------------+------+-----+---------+-------+
| 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) ,正在显示总比赛距离(球员离场),总进球数,总助攻数,总黄牌数和总红牌数。
我正在使用的以下代码已经显示了总得分,但它总结了所有球员得分的目标数量。我想让它显示每个玩家的每个细节的总和。
$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>";
答案 0 :(得分:1)
添加GROUP BY
子句:
SELECT SUM(goals) AS goals, player_id
FROM matchdetails
GROUP BY player_id
ORDER BY player_id ASC
答案 1 :(得分: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>";