Php 如何从mysql足球[足球]结果表动态输出排名表?

Php 如何从mysql足球[足球]结果表动态输出排名表?,php,mysql,sql-order-by,Php,Mysql,Sql Order By,我一直试图找到一些关于这个主题的东西,但我似乎什么也找不到,这里有一些问题,但它们对我的特定项目不起作用 我问了一个关于更新表的类似问题,但它不会满足我的实际需要 以下是结果列表 -------------------------------------------------------- |id | hometeam |goalsfor|goalsagainst| awayteam | ------------------------------------------

我一直试图找到一些关于这个主题的东西,但我似乎什么也找不到,这里有一些问题,但它们对我的特定项目不起作用

我问了一个关于更新表的类似问题,但它不会满足我的实际需要 以下是结果列表

 --------------------------------------------------------
|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得分=得分差Pts=得分

我认为最有效的方法是分配胜利、平局和失利,将进球数和进球数相加,并在回显数据时计算总比赛次数和得分


但我该如何分配赢家、平局或输家呢?然后计算得分和对手得分?

首先将得分表合并在一起,将主队与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;
//连接内容
$sql='select*from matchesTable';
$result=mysql\u查询($sql)
$standings=array();
$standingTemplate=array('matches'=>0,'wins'=>0,'draws'=>0,'loss'=>0,'goalsfor'=>0,'goalscountry'=>0,'goalsdiff'=>0,'points'=>0);
while($row=mysql\u fetch\u assoc($result))
{
handleMatch($row['hometeam']、$row['goalsfor']、$row['GoalsCountry']);
handleMatch($row['awayteam']、$row['goalsfor']、$row['goalscountry']);
print_r(usort(standings,'comparePoints'));//由您自己设置输出格式
}
函数handleMatch($team、$goalsfor、$GoalsFrom)
{
全球$standings,$standingTemplate;
如果($goalsfor>$goalsfrom)
{
$points=3;
$win=1;
$draw=0;
$loss=0;
}
elsif($goalsfor==$goalsfrom)
{
$points=1;
$win=0;
$draw=1;
$loss=0;
}
其他的
{
$points=0
$win=0;
$draw=0;
$loss=1;
}
如果(空($standings[$team]))$standing=$standingTemplate;
其他$standing=$standings[$team];
$standing['matches']++;
$standing['wins']+=$win;
$standing['draws']+=$draw;
$standing['loss']+=$loss;
$standing['goalsfor']+=$goalsfor;
$standing['goalscountry']+=$goalscountry;
$standing['goalsdiff']+=$goalsfor-$goalscountry;
$standing['points']+=$points;
$standings[$team]=$standings;
}
函数比较点($a,$b)
{
如果($a['points']=$b['points'])
{
如果($a['goalsdiff']=$b['goalsdiff'])返回0;
回报($a['goalsdiff']<$b['goalsdiff'])?1:-1;
}       
回报($a['points']<$b['points'])?1:-1;
}

注意事项:我没有测试它,可能是一个小错误(一些
$
缺失)。

最近我不得不制作更扩展的datatable版本。虽然它基于我自己的模式,但它可能对某人有用(它基于以前的a'r答案):

选择
团队id团队id,
t、 团队名称,
t、 国家,,
计数(*)匹配项,
总分,
合计,
计数(评分时的案例>考虑)
然后1)赢了,
计数(得分时的情况=考虑
然后1)画,
计数(得分<考虑时的情况)
然后1)输了,
总额(计分)-总额(计分)余额,
总数(
评分>考虑时的案例
然后3
其他0结束
+得分时的情况=考虑
那么1
其他0(结束)点,
计数(地点为“家”时的情况)
然后1)主场比赛,
计数(当地点=主场且得分>让步时)
然后1结束)home_获胜,
计数(地点=主场,得分=让步时的情况)
然后1结束)home_绘制,
计数(地点为“家”且得分让步时的情况)
然后1)客场获胜,
计数(地点=客场且得分=让步时的情况)
然后1)结束时,画,
计数(地点=客场且得分<让步时的情况)
然后1)远离你输了,
总和(地点='away'时的情况)
然后得分
否则0结束)客场得分,
总和(地点='away'时的情况)
然后考虑
除此之外(0结束),
组(例)
得分>让步时
然后“W”
得分时=让步
那就
当得分<被考虑时
那我
// 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 ;
}
  SELECT
    team_id                                             team_id,
    t.name                                              team_name,
    t.country                                           country,
    count(*)                                            matches,
    SUM(scored)                                         scored_total,
    SUM(conceided)                                      conceided_total,
    count(CASE WHEN scored > conceided
      THEN 1 END)                                       wins,
    count(CASE WHEN scored = conceided
      THEN 1 END)                                       draws,
    count(CASE WHEN scored < conceided
      THEN 1 END)                                       lost,
    sum(scored) - sum(conceided)                        balance,
    sum(
        CASE WHEN scored > conceided
          THEN 3
        ELSE 0 END
        + CASE WHEN scored = conceided
          THEN 1
          ELSE 0 END)                                   points,
    count(CASE WHEN place = 'home'
      THEN 1 END)                                       home_matches,
    count(CASE WHEN place = 'home' AND scored > conceided
      THEN 1 END)                                       home_wins,
    count(CASE WHEN place = 'home' AND scored = conceided
      THEN 1 END)                                       home_draws,
    count(CASE WHEN place = 'home' AND scored < conceided
      THEN 1 END)                                       home_lost,
    SUM(CASE WHEN place = 'home'
      THEN scored
        ELSE 0 END)                                     home_scored,
    SUM(CASE WHEN place = 'home'
      THEN conceided
        ELSE 0 END)                                     home_conceided,
    count(CASE WHEN place = 'away'
      THEN 1 END)                                       away_matches,
    count(CASE WHEN place = 'away' AND scored > conceided
      THEN 1 END)                                       away_wins,
    count(CASE WHEN place = 'away' AND scored = conceided
      THEN 1 END)                                       away_draws,
    count(CASE WHEN place = 'away' AND scored < conceided
      THEN 1 END)                                       away_lost,
    SUM(CASE WHEN place = 'away'
      THEN scored
        ELSE 0 END)                                     away_scored,
    SUM(CASE WHEN place = 'away'
      THEN conceided
        ELSE 0 END)                                     away_conceided,
    GROUP_CONCAT((CASE
                  WHEN scored > conceided
                    THEN 'W'
                  WHEN scored = conceided
                    THEN 'D'
                  WHEN scored < conceided
                    THEN 'L'
                  END) ORDER BY date ASC separator '') streak
  FROM
    (
      (SELECT
         hm.date         date,
         hm.home_team_id team_id,
         hm.score_home   scored,
         hm.score_away   conceided,
         'home'          place
       FROM matches hm
       WHERE hm.season_id = :seasonId)
      UNION ALL
      (SELECT
         am.date         date,
         am.away_team_id team_id,
         am.score_away   scored,
         am.score_home   conceided,
         'away'          place
       FROM matches am
       WHERE am.season_id = :seasonId)
    ) m
    JOIN teams t ON t.id = team_id
  GROUP BY team_id
  ORDER BY points DESC, balance DESC;