2

我目前正在为每个游戏创建 2 行数据,如下所示。然后我可以创建 SQL 代码来生成 W、L、PF、PA 和 diff 列,如图所示。有谁知道如何通过只输入一个游戏结果来创建相同的数据?

谢谢。

id | game | team1     | team2     | score1 | score2 | gameset | diff
--------------------------------------------------------------------
1  | 1    | Reagan    | Jefferson | 43     | 48     | 0       | -5      
2  | 1    | Jefferson | Reagan    | 48     | 43     | 1       |  5

-

id | team       | win | loss | PF | PA | diff
-----------------------------------------------
1  | Jefferson  | 1   | 0    | 48 | 43 |  5
2  | Reagan     | 0   | 1    | 43 | 48 | -5

SQL 代码:

$sql = 'SELECT id, team1, Sum(diff) as tdiff,
               Sum(If(score1>score2,2,0)) + Sum(If(score1=score2,1,0)) +
                   Sum(If(score1+score2=0,-1,0)) AS Pts, 
               Sum(If(score1>score2,1,0)) AS Wins, 
               Sum(If(score1<score2,1,0)) AS Losses, 
               Sum(If(score1+score2=0,-1,0)) + Sum(If(score1=score2,1,0)) AS Ties,
               Sum(score2) AS Allowed, Sum(score1) AS Scored
          FROM database
      GROUP BY team1 ORDER BY Pts DESC, Allowed ASC, Scored DESC, team1 ASC'; 
4

2 回答 2

1

您可以使用两个查询的 UNION 来完成。但是,不清楚id结果中的列应该是什么,所以我把它省略了。赢2分,平1分,负0分不是更常见的吗?您的公式似乎没有为无分数平局提供任何分数。UNION 指定两个分数,“团队分数”或t_score和“对手分数”或o_score。它有效地为表中的每一行创建两行database(这将更传统地命名为game_results- 'database' 应该为一组表保留,而不是为单个表保留)。

SELECT team, SUM(diff) AS tdiff,
       SUM(IF(t_score > o_score, 2, 0)) +
           SUM(IF(t_score = o_score, 1, 0)) +
           SUM(IF(t_score + o_score =0, -1, 0)) AS Pts, 
       SUM(IF(t_score > o_score, 1, 0)) AS Wins, 
       SUM(IF(t_score < o_score, 1, 0)) AS Losses, 
       SUM(IF(t_score + o_score = 0, -1, 0)) +
           SUM(IF(t_score = o_score, 1, 0)) AS Ties,
       SUM(o_score) AS Allowed,
       SUM(t_score) AS Scored
  FROM (SELECT team1 AS team, game, 'H' AS home_away,
               score1 AS t_score, score2 AS o_score
          FROM database
        UNION ALL
        SELECT team2 AS team, game, 'A' AS home_away,
               score2 AS t_score, score1 AS o_score
          FROM database
       ) AS R 
  GROUP BY team ORDER BY Pts DESC, Allowed ASC, Scored DESC, team ASC; 
于 2012-06-22T02:50:44.167 回答
0

这正是我需要的乔纳森!让我走上正轨,让它发挥作用!更新的代码如下

选择团队,SUM(t_score-o_score) 作为 tdiff,
       SUM(IF(t_score > o_score, 2, 0)) +
       SUM(IF(t_score = o_score, 1, 0)) +
       SUM(IF(t_score + o_score =0, -1, 0)) AS Pts,
       SUM(IF(t_score > o_score, 1, 0)) AS Wins,
        SUM(IF(t_score < o_score, 1, 0)) AS Losses,
        SUM(IF(t_score + o_score = 0 , -1, 0)) +
        SUM(IF(t_score = o_score, 1, 0)) AS 平局,
        SUM(o_score) AS 允许,
        SUM(t_score) AS 得分
    FROM (SELECT id, diff, team1 AS team, game, 'H' AS home_away,
        score1 AS t_score, score2 AS o_score
    FROM mn_alldistrictgames
    UNION ALL
        SELECT id, diff, team2 AS team, game, 'A' AS home_away,
        score2 AS t_score , score1 AS o_score
    FROM mn_alldistrictgames
    ) AS R
GROUP BY team ORDER BY Pts DESC, Allowed ASC, Scored DESC, team ASC

于 2012-06-22T15:23:28.007 回答