Mysql 如何通过两列中的外键对列进行求和?

Mysql 如何通过两列中的外键对列进行求和?,mysql,sql,database,Mysql,Sql,Database,目前,我正在使用名为league_standing的表格获得以下结果,并在每场比赛后进行更新。我希望能够对表匹配进行一次查询 两队在主场和客场各打两场。请注意团队id在home\u team\u id和away\u team\u id两列中的位置 以下是我尝试过但失败的内容: select team.name, HomePoints + AwayPoints points from team join ( select team.id, sum(case when ho

目前,我正在使用名为league_standing的表格获得以下结果,并在每场比赛后进行更新。我希望能够对表匹配进行一次查询

两队在主场和客场各打两场。请注意团队id在home\u team\u id和away\u team\u id两列中的位置

以下是我尝试过但失败的内容:

select team.name, 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 else 0 end) AwayPoints
    from team 
    join matches home on team.id = home.home_team_id
    join matches away on 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;
这给了我错误的观点:

这张照片只给了我主队联赛排名的正确结果

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;
如果有帮助,我的数据库模式:


我卡住了!希望您能提供帮助。

我会计算每个球队的主客场成绩,然后进行汇总。 下面的查询应该满足您的需要。您只需将结果与teams表联接,即可获得团队名称。 该语法适用于PostgreSQL,可能您需要更改mysql的某些内容

select team_id, count(*) as P, sum(W) as W, sum(D) as D, sum(L) as L, sum(GF) as GF, sum(GA) as GA, sum(GD) as GD, sum(PTS) as PTS from (
select home_team_id as team_id,       
       case when home_score > away_score then 1
            else 0 end as W,
       case when home_score = away_score then 1
            else 0 end as D,
       case when home_score < away_score then 1
            else 0 end as L,
       home_score as GF,
       away_score as GA,
       home_score-away_score as GD,
       case when home_score > away_score then 3
            when home_score = away_score then 1 
            else 0 end as PTS            
from matches
where league_id = 94 and season_id = 82 and confirmed is not null
union all
select away_team_id as team_id,       
       case when home_score < away_score then 1
            else 0 end as W,
       case when home_score = away_score then 1
            else 0 end as D,
       case when home_score > away_score then 1
            else 0 end as L,
       away_score as GF,
       home_score as GA,
       away_score-home_score as GD,
       case when home_score < away_score then 3
            when home_score = away_score then 1 
            else 0 end as PTS            
from matches
where league_id = 94 and season_id = 82 and confirmed is not null
) as results
group by team_id
order by pts DESC,gd DESC,gf DESC;
顺便说一下,我不认为将结果存储在表中并在每次匹配后更新它们是个坏主意。
应该避免总是重新计算相同的结果,特别是如果有许多用户对咨询排名感兴趣。

不要这样做。数据库规范化的规则之一是不存储计算值。您可以通过在其他表上运行select查询来显示排名。@DanBracuk那么对匹配项运行select查询然后使用PHP计算排名是否更好?谢谢!我想我会坚持使用联赛积分表,但我想实现一个功能来显示最近x场比赛的积分,这将是整个赛季的动态,所以我会使用这个!如果Union不允许,如何向该查询添加订单和限制?假设我想根据前5场比赛获得排名?如果你想根据一些比赛计算排名,你不需要限制条款。您需要在where子句中筛选具有特定条件的匹配项,例如。。。哪里如果要计算查询本身中的最后一个游戏,则需要在两个子查询的where子句中插入额外的子查询。我建议不要这样做,而是首先计算您感兴趣的匹配的日期间隔,然后在where子句中添加一个简单的条件。
select team_id, count(*) as P, sum(W) as W, sum(D) as D, sum(L) as L, sum(GF) as GF, sum(GA) as GA, sum(GD) as GD, sum(PTS) as PTS from (
select home_team_id as team_id,       
       case when home_score > away_score then 1
            else 0 end as W,
       case when home_score = away_score then 1
            else 0 end as D,
       case when home_score < away_score then 1
            else 0 end as L,
       home_score as GF,
       away_score as GA,
       home_score-away_score as GD,
       case when home_score > away_score then 3
            when home_score = away_score then 1 
            else 0 end as PTS            
from matches
where league_id = 94 and season_id = 82 and confirmed is not null
union all
select away_team_id as team_id,       
       case when home_score < away_score then 1
            else 0 end as W,
       case when home_score = away_score then 1
            else 0 end as D,
       case when home_score > away_score then 1
            else 0 end as L,
       away_score as GF,
       home_score as GA,
       away_score-home_score as GD,
       case when home_score < away_score then 3
            when home_score = away_score then 1 
            else 0 end as PTS            
from matches
where league_id = 94 and season_id = 82 and confirmed is not null
) as results
group by team_id
order by pts DESC,gd DESC,gf DESC;