我有一个幻想曲棍球联盟的php数据库,我想在几年内一起添加所有玩家总数,我的例子就在这里。 http://www.cnghl.biz/cnghldb/cnghscorings.php?TeamID=1
所以我想比如让所有的Mike Richards加在一起给我一共GP,G,A,PTS等,所以我可以让一个Mike Richards在一条线上获得他所有的统计数据。有没有办法轻松做到这一点?或者这会变得复杂吗?有人能够给我这些代码来启动它吗?
//Get TeamID from URL
$iTeamID = $_GET["TeamID"];
$iPlayerID= $_GET["PlayerID"];
$iSea=$_GET["Sea"];
$oteaminfo = mysql_query("
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP, SeasonStats.Goals, SeasonStats.Assists, SeasonStats.Points, SeasonStats.Pim, SeasonStats.PlusMinus, SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S, Players.PlayerID
FROM Seasonteam
LEFT JOIN (Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID;
") or die(mysql_error());
Print "<br><br><table border=1 cellpadding=2>";
Print "<td><b><center>Player Name</b></td>";
Print "<td><b><center>GP</center></b></td>";
Print "<td><b><center>G</center></b></td>";
Print "<td><b><center>A</center></b></td>";
Print "<td><b><center>PTS</center></b></td>";
Print "<td><b><center>PIM</center></b></td>";
Print "<td><b><center>+/-</center></b></td>";
Print "<td><b><center>PP</center></b></td>";
Print "<td><b><center>SH</center></b></td>";
Print "<td><b><center>GW</center></b></td>";
Print "<td><b><center>GT</center></b></td>";
Print "<td><b><center>S</center></b></td>";
while($row = mysql_fetch_array($oteaminfo))
{
Print "<tr>";
Print '<td><a href="cnghlplayerinfo.php?PlayerID=' . $row['PlayerID'] . '" style="text-decoration:none;">' . $row['FullName'] . '</a></td>';
Print "<td><center>".$row['GP']."</center></td> ";
Print "<td><center>".$row['Goals']."</center></td> ";
Print "<td><center>".$row['Assists']."</center></td> ";
Print "<td><center>".$row['Points']."</center></td> ";
Print "<td><center>".$row['Pim']."</center></td> ";
Print "<td><center>".$row['PlusMinus']."</center></td> ";
Print "<td><center>".$row['PP']."</center></td> ";
Print "<td><center>".$row['SH']."</center></td> ";
Print "<td><center>".$row['GW']."</center></td> ";
Print "<td><center>".$row['GT']."</center></td> ";
Print "<td><center>".$row['S']."</center></td> ";
Print "</tr>";
}
Print "</table>";
答案 0 :(得分:0)
看起来您应该能够使用GROUP BY为每个播放器获取1行,然后使用聚合SUM函数来获取这些统计信息的总数。下面,我将展示如何将它们聚合为目标,助攻和积分。看一下Group BY子句和聚合函数:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT Players.FullName, Seasonteam.TeamID, SeasonStats.Sea, SeasonStats.GP,
SUM(SeasonStats.Goals) AS totalGoals, SUM(SeasonStats.Assists) AS totalAssists,
SUM(SeasonStats.Points) AS totalPoints, SeasonStats.Pim, SeasonStats.PlusMinus,
SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S,
Players.PlayerID
FROM Seasonteam
LEFT JOIN (Players
LEFT JOIN SeasonStats ON Players.PlayerID = SeasonStats.PlayerID)
ON Seasonteam.TeamID = SeasonStats.TeamID
WHERE Seasonteam.TeamID=$iTeamID
GROUP BY Players.PlayerID;
还要确保转义输入以防止SQL注入:http://php.net/manual/en/security.database.sql-injection.php
我建议你使用mysqli和预备语句,而不是弃用的mysql函数。 http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php