MySql 选择和 SUM 问题
MySql Selection and SUM issues
大家早上好,
我的运动成绩 table 遇到了一些问题,我确信这些问题很简单,但我似乎无法理解为什么它们不是正在工作。
a) 仅选择 2014 赛季。
对于 a) 我假设它应该只是一个 WHERE Season = '2014',但它抛出了:Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
。我也试过 = 2014 因为 table 是一个整数,但得到相同的结果。
在我拔掉所有头发之前,非常感谢任何帮助! :)
$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;
");
您正在将 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;
");
您所做的事情不需要两级子查询。 (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
。这将帮助您在缺少逗号时发现错误。
大家早上好,
我的运动成绩 table 遇到了一些问题,我确信这些问题很简单,但我似乎无法理解为什么它们不是正在工作。
a) 仅选择 2014 赛季。
对于 a) 我假设它应该只是一个 WHERE Season = '2014',但它抛出了:Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
。我也试过 = 2014 因为 table 是一个整数,但得到相同的结果。
在我拔掉所有头发之前,非常感谢任何帮助! :)
$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;
");
您正在将 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;
");
您所做的事情不需要两级子查询。 (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
。这将帮助您在缺少逗号时发现错误。