将多个查询组合成一个查询时,如何将列汇总在一起

Jon*_*han 6 mysql sql union-all

我正在尝试从匹配表中计算联赛积分榜.

+----------------------------------+
|              Matches             |
+----------------------------------+
| id                               |
| league_id (FK League)            |   
| season_id (FK Season)            |
| home_team_id (FK Team)           |
| away_team_id (FK Team)           | 
| home_score                       |
| away_score                       |
| confirmed                        |
+----------------------------------+
Run Code Online (Sandbox Code Playgroud)

我可以使用此查询正确计算Home League Standings:

SELECT team.name, home_team_id AS team_id,
    COUNT(*) AS played,
    SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS won,
    SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS lost,
    SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) AS drawn,
    SUM(home_score) AS goalsFor,
    SUM(away_score) AS goalsAgainst,
    SUM(home_score - away_score) AS goalDifference,
    SUM((CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.home_team_id = team.id
WHERE league_id = 94
    AND season_id = 82
    AND confirmed IS NOT NULL
GROUP BY home_team_id
ORDER BY POINTS DESC;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

和Away League Standigns使用此查询:

SELECT team.name, away_team_id AS team_id,
    COUNT(*) AS played,
    SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS won,
    SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS lost,
    SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) as drawn,
    SUM(away_score) AS goalsFor,
    SUM(home_score) AS goalsAgainst,
    SUM(away_score - home_score) AS goalDifference,
    SUM((CASE WHEN away_score > home_score THEN 3 WHEN away_score = home_score THEN 1 ELSE 0 END)) AS points
FROM matches
INNER JOIN team ON matches.away_team_id = team.id
WHERE league_id = 94
    AND season_id = 82
    AND confirmed IS NOT NULL
GROUP BY away_team_id
ORDER BY points DESC;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是使用UNION ALL组合这两个查询我得不到正确的结果

SELECT * FROM 
(
    SELECT team.name, home_team_id AS team_id,
        COUNT(*) AS played,
        SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS won,
        SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS lost,
        SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) AS drawn,
        SUM(home_score) AS goalsFor,
        SUM(away_score) AS goalsAgainst,
        SUM(home_score - away_score) AS goalDifference,
        SUM((CASE WHEN home_score > away_score THEN 3 WHEN home_score = away_score THEN 1 ELSE 0 END)) AS points
    FROM matches
    INNER JOIN team ON matches.home_team_id = team.id
    WHERE league_id = 94
        AND season_id = 82
        AND confirmed IS NOT NULL
    GROUP BY home_team_id
UNION
    SELECT team.name, away_team_id AS team_id,
        COUNT(*) AS played,
        SUM((CASE WHEN away_score > home_score THEN 1 ELSE 0 END)) AS won,
        SUM((CASE WHEN home_score > away_score THEN 1 ELSE 0 END)) AS lost,
        SUM((CASE WHEN home_score = away_score THEN 1 ELSE 0 END)) as drawn,
        SUM(away_score) AS goalsFor,
        SUM(home_score) AS goalsAgainst,
        SUM(away_score - home_score) AS goalDifference,
        SUM((CASE WHEN away_score > home_score THEN 3 WHEN away_score = home_score THEN 1 ELSE 0 END)) AS points
    FROM matches
    INNER JOIN team ON matches.away_team_id = team.id
    WHERE league_id = 94
        AND season_id = 82
        AND confirmed IS NOT NULL
    GROUP BY away_team_id
) x 
GROUP BY team_id
ORDER BY points DESC;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这应该是预期的结果:

在此输入图像描述

对我做错了什么的想法?谢谢!

更新1:

尝试Dans查询不起作用:

从团队加入中选择team.name,HomePoints + AwayPoints点(选择team.id,总和(当home.home_score> home.away_score然后3时home.home_score = home.away_score然后1其他0结束时的情况)HomePoints,sum(case当away.away_score> away.home_score然后3其他0结束)来自团队加入的AwayPoints匹配home.id = home.home_team_id加入比赛,在team.id = away.away_team_id WHERE home.league_id = 94 AND home.season_id = 82 AND home.confirmed IS not NULL group by id)temp on team.id = temp.id order by points desc;

我得到这个结果:

在此输入图像描述

Dan*_*cuk 0

我会使用这种方法。我只做要点。对于您想要的其他东西,逻辑是相同的。

select TeamName, HomePoints + AwayPoints points
from team join (
select team_id
, sum(case when home.home_score > home.away_score then 3
when home.home_score = home.away_score then 1 else 0 end) HomePoints
, sum(case when away.away_score > away.home_score then 3
when away.home_score = away.away_score then 1 else 0 end) AwayPoints
from team join matches home on team.team_id = home.home_team_id
join matches away on team.team_id = away.away_team_id
where blah blah blah
group by team_id
) temp on team.team_id = temp.team_id
order by points desc
Run Code Online (Sandbox Code Playgroud)