mysql - 如何通过两列中的外键对列求和?

标签 mysql sql database

目前,我正在使用一个名为 league_standing 的表并在每场比赛后更新它来实现下面的结果。我希望能够对表 matches 进行一次查询。

enter image description here

Teams 在主场和客场两场比赛。注意 team_idhome_team_idaway_team_id 两列中的表现

+----------------------------------+
|              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                        |
+----------------------------------+

这是我尝试过但失败的:

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;

它给了我错误的分数:

enter image description here

这只给我主队联赛排名的正确结果

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;

enter image description here

如果有帮助,我的数据库架构:

enter image description here

我卡住了!希望你能帮上忙。

最佳答案

我会计算每支球队的主客场成绩,然后进行汇总。 下面的查询应该做你需要的。您只需将结果与团队表连接起来即可获得团队名称。 该语法适用于 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;

顺便说一句,我不认为将结果存储在表中并在每次比赛后更新它们是个坏主意。 应该避免重新计算总是相同的结果,尤其是当有很多用户有兴趣咨询排名时。

关于mysql - 如何通过两列中的外键对列求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476959/

相关文章:

c# - 如果创建出错,删除数据库

mysql - "insert...where not exists"语句中的重复列名

php - 使用 SQL 生成的名称在 PHP 中链接

sql - 使用 switch-case 语句更新 T-SQL

php - 如何对行进行排序,然后对它们进行分组,然后对组进行排序

.net - 具有 .NET XML 支持的嵌入式 DBMS?

mysql - sql 查询来计算标记帖子的数量

database - 如何在 Redash 中配置 Apache Ignite

php - 循环随机字符串

mysql - 编写 PHP Mysql 查询