目前,我正在使用名为league_standing
的表格实现以下结果,并在每次匹配后更新。我希望能够对表matches
进行一次查询。
Teams
在主场和客场两场比赛。请注意team_id
和home_team_id
away_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;
如果有帮助,我的数据库架构:
我被困住了!希望你能帮忙。
答案 0 :(得分:1)
我会计算每个团队的主客场结果,然后进行聚合。 以下查询应该执行您需要的操作。您只需将结果与团队表一起加入即可获得团队名称。 语法适用于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;
顺便说一句,我不认为将结果存储在表格中并在每次比赛后更新它们是一个坏主意。 重新计算始终是相同的结果是应该避免的,特别是如果有许多用户有兴趣咨询排名。