Aasim Azam Aasim Azam - 6 months ago 24
PHP Question

how to output a standings table on the fly from a mysql table of football [soccer] results?

I have been trying to find something about this topic and I can't seem to find anything, there were a few questions on here but they didn't work for my particular project.

I asked a similar question about updating the table but its not going to work for what I actually want
here is the list of result.

--------------------------------------------------------
|id | hometeam |goalsfor|goalsagainst| awayteam |
--------------------------------------------------------
| 1 |Inter Milan | 3 | 1 | FC Barcelona |
--------------------------------------------------------
| 2 |FC Barcelona | 1 | 0 | Inter Milan |
--------------------------------------------------------
| 3 |Inter Milan | 4 | 0 | AC Milan |
--------------------------------------------------------
| 4 |AC Milan | 0 | 2 | Inter Milan |
--------------------------------------------------------
| 5 |Real Madrid | 2 | 0 | AC Milan |
--------------------------------------------------------
| 6 |AC Milan | 2 | 2 | Real Madrid |
--------------------------------------------------------
| 7 |FC Barcelona | 2 | 2 | AC Milan |
--------------------------------------------------------
| 8 |Real Madrid | 2 | 0 | Inter Milan |
--------------------------------------------------------
| 9 |Inter Milan | 3 | 1 | Real Madrid |
--------------------------------------------------------
| 10 |FC Barcelona | 2 | 0 | Real Madrid |
--------------------------------------------------------
| 11 |Real Madrid | 1 | 1 | FC Barcelona |
--------------------------------------------------------


Basically I want to be able to create a standings table ranking the teams in order, I want to present this table on the fly and not put it into the database

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


POS=Position W=won D=Draw L=Loss F=Goals scored For A=Goals scored against GD=Goals difference Pts=Points

I think the most efficient way to do this would be to assign wins, draws and losses, sum the goals scored and goals scored against and when echoing out the data - calculate the total number of games played and the points.

But how would I assign wins draws or losses? And calculate the goals scored and goals against?

Answer

First union the scores table together swapping the hometeam with the awayteam and swapping the goal counts. This gives you some source data that is easily aggregated and the query to generate the score card is something like this:

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;
Comments