带有主页和离开统计数据的mysql足球排名榜

时间:2015-12-28 13:38:40

标签: php mysql aggregate

我在mysql数据库中有以下数据,还有额外的字段,如裁判,黄牌,红牌,主队射门,客场射门,主场射门,远射命中率。

 ------------------------------------------------------------------------
|id  |   hometeam   |  homegoals  |  awaygoals  |  awayteam  | results  |
 ------------------------------------------------------------------------
| 1  |Brighton      |   3         |     1       | Nott'm Frst|    H     |
 ------------------------------------------------------------------------
| 2  |Birmingham    |   1         |     3       | Reading    |    A     |
 ------------------------------------------------------------------------
| 3  |Nott'm frst   |   4         |     4       | Brighton   |    D     |
 ------------------------------------------------------------------------

我想输出的是一个包含以下详细信息的排名表

 --------------------------------------------------------------
|                   Overall    |   Away    | Home             |
 --------------------------------------------------------------
|pos  |   team   |P|W|D|L|F|A| |W|D|L|F|A| |W|D|L|F|A| |GD|Pts|
 --------------------------------------------------------------
| 1   |Brighton  |2|1|1|0|7|4| |0|1|0|4|4| |1|0|0|3|1| |-1| 4 |
 --------------------------------------------------------------
| 2   |Nott'm fst|2|0|1|1|5|3| |0|0|1|1|3| |0|1|0|4|4| |-1| 1 |
 --------------------------------------------------------------

其他信息可以通过访问每个团队页面来实现。我的查询适用于整个部分,但是,其他细节变得难以获得。

查询如下

select 
    team, 
    count(*) P, 
    count(case when fulltimehomegoals > fulltimeawaygoals then 1 end) W, 
    count(case when fulltimeawaygoals> fulltimehomegoals then 1 end) L, 
    count(case when fulltimehomegoals = fulltimeawaygoals then 1 end) D, 
    sum(fulltimehomegoals) F, 
    sum(fulltimeawaygoals) A, 
    sum(fulltimehomegoals) - sum(fulltimeawaygoals) GD,
    sum(
          case when fulltimehomegoals > fulltimeawaygoals then 3 else 0 end 
        + case when fulltimehomegoals = fulltimeawaygoals then 1 else 0 end
    ) Pts 
from (
    select hometeam team, fulltimehomegoals, fulltimeawaygoals, fulltimeresults from fixtures 
  union all
    select awayteam, fulltimeawaygoals, fulltimehomegoals, fulltimeresults from fixtures
) a 
group by team
order by Pts desc, GD desc;

请为需要编辑的部分指明方向以达到最终结果。

提前谢谢。

0 个答案:

没有答案