Jon*_*han 5 mysql sql database
目前我正在使用一个名为的表来实现下面的结果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 |
+----------------------------------+
Run Code Online (Sandbox Code Playgroud)
这是我尝试但失败的原因:
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;
Run Code Online (Sandbox Code Playgroud)
这给了我错误的观点:

而且这个给了我正确的主队联赛排名结果
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)

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

我被卡住了!希望你能帮忙.
小智 2
我会计算每个球队的主客场结果,然后进行汇总。以下查询应该可以满足您的需要。您只需将结果与球队表连接起来即可获得球队名称。该语法适用于 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;
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我不认为将结果存储在表中并在每场比赛后更新它们是一个坏主意。应该避免总是重新计算相同的结果,特别是当有很多用户有兴趣查询排名时。
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |