按 H2H 排名 table 足球比赛
Rank standings table of soccer matches by H2H
让我开始用一个例子来解释这个,我有一个 table,里面有足球联赛的比赛记录,通过使用这个 table,它的比赛结果可以生成一个排名table 通过 mysql 查询查询此联盟中的球队。
Table [匹配项](示例)
--------------------------------------------------------
|id | hometeam |goalsfor|goalsagainst| awayteam |
--------------------------------------------------------
--------------------------------------------------------
| 8 | Real Madrid | 2 | 0 | Inter Milan |
--------------------------------------------------------
| 9 | Inter Milan | 3 | 3 | Real Madrid |
--------------------------------------------------------
通过查询生成排名
Pos Team Pld W D L F A GD Pts
1 FC Barcelona 5 2 3 0 8 5 3 9
2 Inter Milan 6 2 2 2 11 10 1 8
3 Real Madrid 6 2 2 2 8 8 0 8
4 AC Milan 5 0 3 2 8 12 -4 3
查询:
select
team,
count(*) played,
count(case when goalsfor > goalsagainst then 1 end) wins,
count(case when goalsagainst> goalsfor then 1 end) lost,
count(case when goalsfor = goalsagainst then 1 end) draws,
sum(goalsfor) goalsfor,
sum(goalsagainst) goalsagainst,
sum(goalsfor) - sum(goalsagainst) goal_diff,
sum(
case when goalsfor > goalsagainst then 3 else 0 end
+ case when goalsfor = goalsagainst then 1 else 0 end
) score
from (
select hometeam team, goalsfor, goalsagainst from scores
union all
select awayteam, goalsagainst, goalsfor from scores
) a
group by team
order by score desc, goal_diff desc;
我想做的是根据头对头比赛对积分榜进行排序,所以它会首先按积分排序,然后如果有平局则进行第二次排序将是查看两支球队的比赛并比较谁比另一支获胜更多或得分更多,然后用它来对 table.
进行排序
如示例中那样,皇家马德里将排名第 2,然后国际米兰排名第 3。
我怎样才能做到这一点?
我想在两队积分相等的情况下比较两队的比赛,并以此来排序。
ORDER BY score DESC, h2h DESC; goal_diff DESC
Update: I ended going with a solution mix of sql and php, first I find equaled teams in rank, and then generate mini h2h standings for those team and update the rank based on it. I still see this doable with just sql, but with my heavy query its too complicated to implement with just sql, thats why I mixed with php in the implementation.
您需要分两步处理。首先,运行 上面的查询并将结果存储在一个工作 table 中(下面称之为工作)。然后,您需要为得分相同的每个团队获得决胜局得分。下面,我将比赛 table 加入到每个团队的工作 table 中,并忽略工作行不具有相同分数的任何地方,因为它们并不重要。如果他们赢了,则给团队 1。必须为另一边再做一次。您可能想将其更改为 3 表示获胜,1 表示平局。
将这些结果相加,将结果加入到工作中的团队行中,对于得分相同的每一行,您都有一个平局得分。
你需要看看如果你有很多队得分相同会发生什么,看看这是否是你想要的结果。
select w.*, b.hth
From work w
left outer join (
select team, SUM(hth) hth
from (
Select hometeam team, case when m.goalsfor > m.goalsagainst then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
union all
Select awayteam team, case when m.goalsAgainst > m.goalsFor then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
) a --all hth at same points
group by team
) b --summed to one row per team
on b.team = w.team
order by w.score desc, b.hth desc;
让我开始用一个例子来解释这个,我有一个 table,里面有足球联赛的比赛记录,通过使用这个 table,它的比赛结果可以生成一个排名table 通过 mysql 查询查询此联盟中的球队。
Table [匹配项](示例)
--------------------------------------------------------
|id | hometeam |goalsfor|goalsagainst| awayteam |
--------------------------------------------------------
--------------------------------------------------------
| 8 | Real Madrid | 2 | 0 | Inter Milan |
--------------------------------------------------------
| 9 | Inter Milan | 3 | 3 | Real Madrid |
--------------------------------------------------------
通过查询生成排名
Pos Team Pld W D L F A GD Pts
1 FC Barcelona 5 2 3 0 8 5 3 9
2 Inter Milan 6 2 2 2 11 10 1 8
3 Real Madrid 6 2 2 2 8 8 0 8
4 AC Milan 5 0 3 2 8 12 -4 3
查询:
select
team,
count(*) played,
count(case when goalsfor > goalsagainst then 1 end) wins,
count(case when goalsagainst> goalsfor then 1 end) lost,
count(case when goalsfor = goalsagainst then 1 end) draws,
sum(goalsfor) goalsfor,
sum(goalsagainst) goalsagainst,
sum(goalsfor) - sum(goalsagainst) goal_diff,
sum(
case when goalsfor > goalsagainst then 3 else 0 end
+ case when goalsfor = goalsagainst then 1 else 0 end
) score
from (
select hometeam team, goalsfor, goalsagainst from scores
union all
select awayteam, goalsagainst, goalsfor from scores
) a
group by team
order by score desc, goal_diff desc;
我想做的是根据头对头比赛对积分榜进行排序,所以它会首先按积分排序,然后如果有平局则进行第二次排序将是查看两支球队的比赛并比较谁比另一支获胜更多或得分更多,然后用它来对 table.
进行排序如示例中那样,皇家马德里将排名第 2,然后国际米兰排名第 3。
我怎样才能做到这一点? 我想在两队积分相等的情况下比较两队的比赛,并以此来排序。
ORDER BY score DESC, h2h DESC; goal_diff DESC
Update: I ended going with a solution mix of sql and php, first I find equaled teams in rank, and then generate mini h2h standings for those team and update the rank based on it. I still see this doable with just sql, but with my heavy query its too complicated to implement with just sql, thats why I mixed with php in the implementation.
您需要分两步处理。首先,运行 上面的查询并将结果存储在一个工作 table 中(下面称之为工作)。然后,您需要为得分相同的每个团队获得决胜局得分。下面,我将比赛 table 加入到每个团队的工作 table 中,并忽略工作行不具有相同分数的任何地方,因为它们并不重要。如果他们赢了,则给团队 1。必须为另一边再做一次。您可能想将其更改为 3 表示获胜,1 表示平局。
将这些结果相加,将结果加入到工作中的团队行中,对于得分相同的每一行,您都有一个平局得分。
你需要看看如果你有很多队得分相同会发生什么,看看这是否是你想要的结果。
select w.*, b.hth
From work w
left outer join (
select team, SUM(hth) hth
from (
Select hometeam team, case when m.goalsfor > m.goalsagainst then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
union all
Select awayteam team, case when m.goalsAgainst > m.goalsFor then 1 else 0 end hth
from matches m
inner join work w1 on m.hometeam = w1.team
inner join work w2 on m.awayteam = w2.team
where w1.score = w2.score
) a --all hth at same points
group by team
) b --summed to one row per team
on b.team = w.team
order by w.score desc, b.hth desc;