zoukankan      html  css  js  c++  java
  • 球队以及得分计算的SQL语句

    首先题目是这样的:

    image

    球队表teams

    比赛表matches

    赢了得3分,平局的得1分,输了得0分。

    思路:

    一个球队的成绩分为两部分,作为主队的得分和作为客队的得分;

    计算出一次比赛中具体得了多少分,具体的比较我也不知道;

    得到这个零时表然后根据球队group求和;

    最后根据球队表关联得出球队的名字;

    然后在排个序。

    select teams.*, 
           ISNULL(points.num_points,0) as num_points 
    from teams left join 
        (select host_team,
                SUM(hostpoint) as num_points 
         from (select host_team,
                      (case when host_goals>guest_goals then 3 when host_goals=guest_goals then 1 else 0 end) as hostpoint
               from matches
               union
               select guest_team,
                      (case when host_goals>guest_goals then 0 when host_goals=guest_goals then 1 else 3 end) as guestpoint
               from matches) point 
         group by host_team) points 
    on teams.team_id=points.host_team
    order by points.num_points desc,teams.team_id asc

    1. 我不知道怎么把比赛结果转化为得分,case when还不会用,不知道还有这个

    2. ISNULL也没有判断

  • 相关阅读:
    CentOS7配置VIP
    Linux定时任务crontab命令
    zabbix安装部署
    ansible部署EFK
    Python中参数的冒号与箭头表示注释
    pycharm 进行远程服务器修改与调试
    判别模型与生成模型
    写Python机器学习时的一些注意事项
    numpy基本操作
    手写神经网络
  • 原文地址:https://www.cnblogs.com/tuhooo/p/8641449.html
Copyright © 2011-2022 走看看