2011-11-04 112 views
12

我一直在试图找到关于这个话题的东西,我似乎无法找到任何东西,这里有几个问题,但他们没有为我的特定项目工作。如何从一张足球[足球]的mysql表中输出一个积分榜?

我问了一个类似的问题,关于更新表,但它不会工作,我真的想要 这里是结果列表。

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

基本上,我希望能够以创造一个榜上表排名的球队,我想提出对飞这个表,而不是把它放入数据库

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 =进球差距分=积分

我认为最有效的方法是分配胜利,平局和亏损,总和目标得分和进球得分和回声数据 - 计算总数玩过的游戏和点数。

但是我将如何分配胜负或亏损?并计算得分和进球的目标?

+1

您可以将列标题放在较低的表上,以便我们知道应如何计算每一列? – nickb

+1

您是否有另一张列出团队名称的表格,或者是通过从hometeam和awayteam中选择不同的列表来获得全部表格的唯一方法? – megaflop

+0

@Wazzzy:D这些都是随机游戏的实际结果 - 仅用于示例目的,我会得到很多结果 - 可能在过去的60到70年中有10-15个联赛 - 也许他们都在一个mysql表中可能会一个坏主意,但我会在以后考虑它。但是我可能会根据用户想要的数据添加额外的子句来选择比赛。 –

回答

12

首先将分数表合并在一起,将hometeam与awayteam交换并交换目标计数。这给了你,很容易聚集一些源数据和查询以生成记分卡是这样的:

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; 
+2

不错和优雅:) – roselan

+0

似乎工作,因为它不返回任何错误 - 但只是工作如何回显/打印出数据 - 不够睡觉,所以一切都有点朦胧:S –

+0

它的工作原理!你先生是个天才! –

3
// connection stuff 
$sql = 'select * from matchesTable'; 
$result = mysql_query($sql) 

$standings = array(); 
$standingTemplate = array ('matches' => 0, 'wins' => 0, 'draws' => 0, 'losses' => 0, 'goalsfor' => 0, 'goalsagainst' => 0, 'goalsdiff' => 0, 'points' => 0); 

while ($row = mysql_fetch_assoc($result)) 
{ 
    handleMatch($row['hometeam'], $row['goalsfor'], $row['goalsagainst']); 
    handleMatch($row['awayteam'], $row['goalsfor'], $row['goalsagainst']); 

    print_r(usort(standings, 'comparePoints')); // up to you to format the output as you like 
} 

function handleMatch($team, $goalsfor, $goalsagainst) 
{ 
    global $standings, $standingTemplate; 
    if ($goalsfor > $goalsagainst) 
    { 
     $points = 3; 
     $win = 1; 
     $draw = 0; 
     $loss = 0; 
    } 
    elsif ($goalsfor == $goalsagainst) 
    { 
     $points = 1; 
     $win = 0; 
     $draw = 1; 
     $loss = 0; 
    } 
    else 
    { 
     $points = 0 
     $win = 0; 
     $draw = 0; 
     $loss = 1; 
    } 

    if (empty($standings[$team]))$standing = $standingTemplate; 
    else $standing = $standings[$team]; 

    $standing['matches']++; 
    $standing['wins'] += $win; 
    $standing['draws'] += $draw; 
    $standing['losses'] += $loss; 
    $standing['goalsfor'] += $goalsfor; 
    $standing['goalsagainst'] += $goalsagainst; 
    $standing['goalsdiff'] += $goalsfor - $goalsagainst; 
    $standing['points'] += $points; 

    $standings[$team] = $standing; 

} 

function comparePoints($a, $b) 
{ 
    if ($a['points'] == $b['points']) 
    { 
     if ($a['goalsdiff'] == $b['goalsdiff']) return 0; 
     return ($a['goalsdiff'] < $b['goalsdiff']) ? 1 : -1 ; 
    }  
    return ($a['points'] < $b['points']) ? 1 : -1 ; 
} 

注意:我没有测试它和所有,可能是小虫子(有些$;失踪)。

+0

就像你说的一些小错误,但是很容易修复和代码工作..很酷.. thnx。 – fjckls