目前,我正在使用名为league_standing的表来实现以下结果,并在每次匹配后对其进行更新。我希望能够对表matches进行一次查询。
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;

这给了我错误的观点:

这给了我仅主队联赛排名的正确结果
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;

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

10-07 12:38