drjltlm156790 2014-12-22 23:50
浏览 74
已采纳

如何在MySQL中订购体育联赛表?

I already have a league standings table, however, I want to make it take into account that 2 draws are equal to a win, and in a tie, lowest points against takes precedence.

Currently shows like below, but technically Team2 should have won:

Team    W   L   T   PF  PA  PCT 
—————————————————————————————————
Team1   7   3   0   247 139 0.7000
Team2   6   2   2   220 122 0.6000 
Team3   6   4   0   191 191 0.6000
Team4   4   5   1   167 201 0.4000 
Team5   3   6   1   142 202 0.3000
Team6   2   8   0   193 305 0.2000

This is the SQL:

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 HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
    FROM Game
    union all select AwayTeam, AwayScore, HomeScore
    FROM Game
   ) a
) b
GROUP BY team
ORDER BY wins DESC, draws DESC, lost ASC, goal_diff DESC;
  • 写回答

2条回答 默认 最新

  • dongli1887 2014-12-23 00:01
    关注

    Take a look at your ORDER BY clause: you are effectively asking for the results to be ordered by order of wins descending. This rule takes preference over all the others, so obviously Team1 wins.

    ORDER BY wins DESC, draws DESC, lost ASC, goal_diff DESC;
    

    I want to make it take into account that 2 draws are equal to a win, and in a tie, lowest points against takes precedence.

    Then that would be:

    ORDER BY (wins*2 + draws) DESC, lost ASC, ptsagainst DESC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效