下午,
我为我所在的团队创建了排名表。
详细信息是从“游戏”数据库中提取的,该数据库记录了所有比赛,日期,主场/客场得分和赛季,然后动态创建表格。
最近,抢劫犯已更改为:
我正在努力地思考如何分组/订购以符合这些条件。
表模式(GameId是每个球队名称的前3个字符的组合,主队例如底特律vs芝加哥将是DETCHI,然后是客队CHIDET):
GameId varchar(10)
GameDate date NULL
GameHomeTeam varchar(100) NULL
GameHomeScore int(4) NULL
GameAwayTeam varchar(100) NULL
GameAwayScore int(4) NULL
GameSeason int(4)
我的查询
global $wpdb;
$year = "2018";
$query ="SELECT team
,COUNT(*) played
,SUM(win) wins
,SUM(loss) lost
,SUM(win)/count(*) pctWon
,SUM(draw) draws
,SUM(SelfScore) ptsfor
,SUM(OpponentScore) ptsagainst
,SUM(SelfScore) - SUM(OpponentScore) goal_diff
,SUM(3*win + draw) score
FROM (
SELECT team
,SelfScore
,OpponentScore
,SelfScore > OpponentScore win
,SelfScore < OpponentScore loss
,SelfScore = OpponentScore draw
FROM (
SELECT `GameHomeTeam` team,
`GameHomeScore` SelfScore,
`GameAwayScore` OpponentScore
FROM Game
WHERE `GameSeason` = '" . $year . "'
UNION ALL
SELECT `GameAwayTeam`,
`GameAwayScore`,
`GameHomeScore`
FROM Game
WHERE `GameSeason` = '" . $year . "'
) a
) b
GROUP BY team
ORDER BY wins DESC, draws DESC, lost ASC, goal_diff DESC";
非常感谢任何帮助。