新手需要MYSQL帮助

时间:2014-03-11 02:25:05

标签: mysql

我正在尝试运行查询以显示AFL足球赛季的阶梯。

这是我目前的代码

SELECT Team, 
    COUNT(Total) AS P, 
    (SELECT COUNT(WL)
     FROM MatchDetails
     WHERE Season = 1898 AND WL = "W") AS Won,
    (SELECT COUNT(WL)
     FROM MatchDetails
     WHERE Season = 1898 AND WL = "L") AS Lost,
    (SELECT COUNT(WL)
     FROM MatchDetails
     WHERE Season = 1898 AND WL = "D") AS Draw,
    SUM(Goals), 
    SUM(Behinds)

FROM `MatchDetails` 
WHERE Season = 1898
GROUP BY Team

输出是;

Team| Played|Won|Lost|Draw|Goals|Behinds|
Team A| 16| 31| 31| 2| 95| 122|
Team B| 16| 31| 31| 2| 55| 82|
Team C| 16| 31| 31| 2| 88| 115|
Team D| 16| 31| 31| 2| 113 | 92|

问题是,赢取,失去和平局列的输出是计算本赛季赢,输和赢的游戏总数,而不是每个团队的数字W,L,D。我已尝试在子查询中使用GROUP BY Team,但我收到错误消息“#2014 - 命令不同步;您现在无法运行此命令”

这是输出的外观;

Team| Played|Won|Lost|Draw|Goals|Behinds|
Team A| 16| 1| 15| 0| 95| 122|
Team B| 16| 11| 4| 1| 55| 82|
Team C| 16| 14| 1| 1| 88| 115|
Team D| 16| 5| 11| 0| 113| 92|

任何人都可以为我提供解决方案吗?

1 个答案:

答案 0 :(得分:1)

SELECT Team,
       COUNT(*) AS Played,
       SUM(WL = 'W') AS Won,
       SUM(WL = 'L') AS Lost,
       SUM(WL = 'D') AS Draw,
       SUM(Goals) AS Goals,
       SUM(Behinds) AS Behinds
FROM MatchDetails
WHERE Season = 1898
GROUP BY Team

您的SELECT COUNT子查询不是相关的子查询,因此他们会计算表中的所有内容,而不是该行的团队。