2

以下に示すように、現在、各ゲームに対して2行のデータを作成しています。次に、示されているように、W、L、PF、PA、および diff 列を生成する SQL コードを作成できます。1つのゲーム結果を入力するだけで同じデータを作成する方法を知っている人はいますか?

ありがとう。

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

2 つのクエリの UNION を使用して実行できます。ただし、id結果の列がどうあるべきかは明確ではないため、省略しました。勝てば2点、引き分けで1点、負けで0点というのが普通ではないでしょうか。あなたの式は、スコアレスドローのポイントを与えないようです。UNION は、「チーム スコア」またはt_score「対戦相手のスコア」または「対戦相手のスコア」の2 つのスコアを指定しo_scoreます。テーブルの各行に対して効果的に 2 つの行を作成しdatabaseます (これは、慣習的に次のような名前が付けられgame_resultsます。「データベース」は、個々のテーブルではなく、テーブルのコレクション用に予約する必要があります)。

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 勝ち、SUM(IF(t_score > o_score, 1, 0)) AS 勝ち、
        SUM(IF(t_score < o_score, 1, 0)) AS 負け、
        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, 許可された ASC, Scored DESC, team ASC

于 2012-06-22T15:23:28.007 に答える