如何从足球[soccer]结果的mysql表中即时输出排行榜? [英] how to output a standings table on the fly from a mysql table of football [soccer] results?

查看:114
本文介绍了如何从足球[soccer]结果的mysql表中即时输出排行榜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到有关此主题的内容,但似乎找不到任何东西,这里有一些问题,但是它们不适用于我的特定项目.

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 =位置W =获胜D =平局L =亏损F =目标得分A =目标得分GD =目标差Pts =积分

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?

推荐答案

首先将得分表合并在一起,将主队与客队交换并交换进球数.这样可以为您提供一些易于汇总的源数据,并且用于生成记分卡的查询如下所示:

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;

这篇关于如何从足球[soccer]结果的mysql表中即时输出排行榜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆