在PHP中添加列

时间:2013-09-10 00:17:52

标签: php mysql sql

我正在尝试将列添加到一起以在每列的末尾获得总计,我搜索了这个主题并试图按照它说的做但却出错了

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近'SELECT SUM(Sea)FROM PlayerStats;来自SeasonStats WHERE   SeasonStats.PlayerID ='第2行

我对此很陌生,似乎无法弄清楚如何让它发挥作用。任何帮助将不胜感激。

$oPlayerStats = mysql_query("
SELECT SeasonStats.Sea, SeasonStats.Team, SeasonStats.GP, SeasonStats.Goals,     SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus, SeasonStats.Pim,   SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S
SELECT SUM(Sea) FROM PlayerStats;
FROM SeasonStats
WHERE SeasonStats.PlayerID=$iPlayerID
ORDER BY SeasonStats.Sea;
") or die(mysql_error());

echo "<br><br><table border=1 cellpadding=1>";

echo "<tr>";
echo "<td>Season</td> ";
echo "<td>Team</td> ";
echo "<td>GP</td> ";
echo "<td>G</td> ";
echo "<td>A</td> ";
echo "<td>P</td> ";
echo "<td>PIM</td> ";
echo "<td>+/-</td> ";
echo "<td>PPG</td> ";
echo "<td>SHG</td> ";
echo "<td>GWG</td> ";
echo "<td>GTG</td> ";
echo "<td>Shots</td> ";

while($row = mysql_fetch_array($oPlayerStats))
{
echo "<tr>";
echo "<td>".$row['Sea']."</td> ";
echo "<td>".$row['Team']."</td> ";
echo "<td>".$row['GP']."</td> ";
echo "<td>".$row['Goals']."</td> ";
echo "<td>".$row['Assists']."</td> ";
echo "<td>".$row['Points']."</td> ";
echo "<td>".$row['Pim']."</td> ";
echo "<td>".$row['PlusMinus']."</td> ";
echo "<td>".$row['PP']."</td> ";
echo "<td>".$row['SH']."</td> ";
echo "<td>".$row['GW']."</td> ";
echo "<td>".$row['GT']."</td> ";
echo "<td>".$row['S']."</td> ";
$result = mysql_query('SELECT SUM(Sea) AS value_sum FROM codes'); 
    $row = mysql_fetch_assoc($result); 
    $sum = $row['value_sum'];

2 个答案:

答案 0 :(得分:0)

我不知道您的数据库架构是什么样的,但您可能希望修改原始查询,如下所示:

SELECT SeasonStats.Sea, SeasonStats.Team, SeasonStats.GP, SeasonStats.Goals,
SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus, SeasonStats.Pim,
SeasonStats.PP, SeasonStats.SH, SeasonStats.GW, SeasonStats.GT, SeasonStats.S,
SUM(SeasonStats.Sea) as total    
FROM PlayerStats
   INNER JOIN SeasonStats ON PlayerStats.PlayerID = SeasonStats.PlayerID
WHERE SeasonStats.PlayerID = $iPlayerID
ORDER BY SeasonStats.Sea;

您可能还希望使用GROUP BY CLAUSE在SQL中添加一些列。

答案 1 :(得分:0)

您在摘要查询中混合了摘要查询。您需要删除该行 - 第一个查询应该只是:

SELECT SeasonStats.Sea, SeasonStats.Team, SeasonStats.GP, SeasonStats.Goals,
    SeasonStats.Assists, SeasonStats.Points, SeasonStats.PlusMinus,
    SeasonStats.Pim,   SeasonStats.PP, SeasonStats.SH, SeasonStats.GW,
    SeasonStats.GT, SeasonStats.S
FROM SeasonStats
WHERE SeasonStats.PlayerID=$iPlayerID
ORDER BY SeasonStats.Sea;