MySql选择和SUM问题

时间:2015-01-03 12:04:57

标签: mysql sql sum where division

早上好,

我有一个运动成绩表,我有几个问题,我肯定很简单,但我似乎无法理解为什么他们不工作。

a)仅选择2014赛季。

对于a)我认为它应该只是一个WHERE Season ='2014',但它会抛出:Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in。我也试过= 2014,因为表是一个int,但是得到了相同的结果。

在我拔掉所有头发之前,任何帮助都非常感谢! :)

$result = mysql_query("
SELECT team
     , COUNT(*)  played
     , SUM(win)  wins
     , SUM(loss) lost
     , SUM(win)/count(*) pctWon1
     , SUM(draw) draws
     , SUM(SelfScore) ptsfor
     , SUM(OpponentScore) ptsagainst
     , SUM(SelfScore) - SUM(OpponentScore) goal_diff
     , SUM((win*2 + draw)- loss) score
     , SUM(win*2 + draw)/count(*) pctWon

FROM (
      SELECT team
     , SelfScore
     , OpponentScore
     , SelfScore > OpponentScore win
     , SelfScore < OpponentScore loss
     , SelfScore = OpponentScore draw
      FROM (
        SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
        FROM Game
        union all select AwayTeam, AwayScore, HomeScore
        FROM Game
       ) a
) b
WHERE Season = '2014'
GROUP BY team
ORDER BY pctWon DESC, score DESC, ptsagainst DESC;
        ");

2 个答案:

答案 0 :(得分:1)

您将where子句应用于外部查询(b),其中没有此类列Season。您应该将其移动到内部查询:

$result = mysql_query("
SELECT team
     , COUNT(*)  played
     , SUM(win)  wins
     , SUM(loss) lost
     , SUM(win)/count(*) pctWon1
     , SUM(draw) draws
     , SUM(SelfScore) ptsfor
     , SUM(OpponentScore) ptsagainst
     , SUM(SelfScore) - SUM(OpponentScore) goal_diff
     , SUM((win*2 + draw)- loss) score
     , SUM(win*2 + draw)/count(*) pctWon

FROM (
      SELECT team
     , SelfScore
     , OpponentScore
     , SelfScore > OpponentScore win
     , SelfScore < OpponentScore loss
     , SelfScore = OpponentScore draw
      FROM (
        SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
        FROM Game
        WHERE Season = '2014'
        UNION ALL 
        SELECT AwayTeam, AwayScore, HomeScore
        FROM Game
        WHERE Season = '2014'
       ) a
) b
GROUP BY team
ORDER BY pctWon DESC, score DESC, ptsagainst DESC;
");

答案 1 :(得分:0)

您不需要两个级别的子查询来处理您正在执行的操作。 (MySQL实际上实现了子查询,增加了性能开销。)但是,如果你这样做,你应该把你想要的所有字段拉出来。

我建议像这样编写查询:

SELECT team, COUNT(*)  played, SUM(IsWin) as wins, SUM(IsLoss) as losses,
       SUM(IsWin)/count(*) as pctWon1,
       SUM(Draw) as draws,
       SUM(SelfScore) as ptsfor, SUM(OpponentScore) as ptsagainst,
       SUM(SelfScore) - SUM(OpponentScore) as goal_diff,
       SUM((IsWin*2 + IsDraw) - IsLoss) as score,
       SUM(IsWin*2 + IsDraw) / count(*) pctWon
FROM (SELECT season, HomeTeam as team, HomeScore as SelfScore, AwayScore as OpponentScore,
             (HomeScore > AwayScore) as IsWin,
             (HomeScore < AwayScore) as IsLoss,
             (HomeScore = AwayScore) as IsDraw
      FROM Game
      UNION ALL
      SELECT Season, AwayTeam, AwayScore, HomeScore,
             (AwayScore > HomeScore) as IsWin,
             (AwayScore < HomeScore) as IsLoss,
             (AwayScore = HomeScore) as IsDraw
      FROM Game
     ) t
WHERE Season = 2014
GROUP BY team
ORDER BY pctWon DESC, score DESC, ptsagainst DESC;

我对三次重复标志的定义并不感到兴奋。并且,在大多数数据库中,我更喜欢在子查询中使用一次逻辑。但是,MySQL作为实际的性能损失,因此最好避免使用。

一个小建议:在定义列别名时使用as。这将帮助您在缺少逗号时找到错误。