我目前正在为每个游戏创建 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';