我有一系列按钮标记。
SELECT team, COUNT( team ) AS pld,
SUM( IF( Pts = 3, 1, 0 ) ) AS W,
SUM( IF( Pts = 1, 1, 0 ) ) AS D,
SUM( IF( Pts = 0, 1, 0 ) ) AS L,
SUM( Pts ) AS Pts,
SUM( GF ) AS GF,
SUM( GA ) AS GA,
SUM( GF ) - SUM( GA ) AS GD
FROM (
SELECT home_team_id AS team,
home_goal_number AS GF,
away_goal_number AS GA,
CASE
WHEN home_goal_number > away_goal_number
THEN 3
WHEN home_goal_number = away_goal_number
THEN 1
ELSE 0
END AS Pts
FROM matches
UNION ALL
SELECT away_team_id AS team,
away_goal_number AS GF,
home_goal_number AS GA,
CASE
WHEN away_goal_number > home_goal_number
THEN 3
WHEN away_goal_number = home_goal_number
THEN 1
ELSE 0
END AS Pts
FROM matches
) AS tab
GROUP BY team
ORDER BY Pts DESC
这项工作完美无缺。只有一个问题。如果两支球队以相同的分数结束联赛,那么消除歧义的第一种方法就是了解谁赢得直接比赛。
如何修改此查询才能执行此操作?
答案 0 :(得分:2)
如果你认为唯一的方法是知道有一个平局是使用你所显示的查询来计算点数,你可以看到将直接匹配获胜者信息添加到当前查询并不是一件容易的事。
我会考虑将您的查询放在VIEW中,然后创建一个新查询以使用该视图并计算总体获胜者,其中两个团队的点数相等。
正如我在评论中所说,当直接比赛是抽签或两支以上球队在比赛结束时得分相同时,你会怎么做?这些情况是可能的,但很难编写解决方案。
另一种方法是使用目标差异,但这可能需要你的联盟改变他们的规则:)