前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode MySQL 1212. 查询球队积分

LeetCode MySQL 1212. 查询球队积分

作者头像
Michael阿明
发布2021-02-19 10:32:41
4610
发布2021-02-19 10:32:41
举报
文章被收录于专栏:Michael阿明学习之路

文章目录

1. 题目

Table: Teams

代码语言:javascript
复制
+---------------+----------+
| Column Name   | Type     |
+---------------+----------+
| team_id       | int      |
| team_name     | varchar  |
+---------------+----------+
此表的主键是 team_id,表中的每一行都代表一支独立足球队。

Table: Matches

代码语言:javascript
复制
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| match_id      | int     |
| host_team     | int     |
| guest_team    | int     | 
| host_goals    | int     |
| guest_goals   | int     |
+---------------+---------+
此表的主键是 match_id,表中的每一行都代表一场已结束的比赛,
比赛的主客队分别由它们自己的 id 表示,
他们的进球由 host_goals 和 guest_goals 分别表示。
 
积分规则如下:

赢一场得三分;
平一场得一分;
输一场不得分。

写出一条SQL语句以查询每个队的 team_id,team_name 和 num_points。 结果根据 num_points 降序排序,如果有两队积分相同,那么这两队按 team_id 升序排序。

查询结果格式如下:

代码语言:javascript
复制
Teams table:
+-----------+--------------+
| team_id   | team_name    |
+-----------+--------------+
| 10        | Leetcode FC  |
| 20        | NewYork FC   |
| 30        | Atlanta FC   |
| 40        | Chicago FC   |
| 50        | Toronto FC   |
+-----------+--------------+

Matches table:
+------------+--------------+---------------+-------------+--------------+
| match_id   | host_team    | guest_team    | host_goals  | guest_goals  |
+------------+--------------+---------------+-------------+--------------+
| 1          | 10           | 20            | 3           | 0            |
| 2          | 30           | 10            | 2           | 2            |
| 3          | 10           | 50            | 5           | 1            |
| 4          | 20           | 30            | 1           | 0            |
| 5          | 50           | 30            | 1           | 0            |
+------------+--------------+---------------+-------------+--------------+

Result table:
+------------+--------------+---------------+
| team_id    | team_name    | num_points    |
+------------+--------------+---------------+
| 10         | Leetcode FC  | 7             |
| 20         | NewYork FC   | 3             |
| 50         | Toronto FC   | 3             |
| 30         | Atlanta FC   | 1             |
| 40         | Chicago FC   | 0             |
+------------+--------------+---------------+

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/team-scores-in-football-tournament 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 按照主队,客队分别计算完 合并,再group by,left join 队名表
代码语言:javascript
复制
# Write your MySQL query statement below
select Teams.team_id, Teams.team_name, ifnull(Scores.score, 0) num_points
from Teams left join
(
    select team, sum(score) score
    from
    (
        (
            select host_team team, (case when host_goals > guest_goals then 3
                                    when host_goals = guest_goals then 1
                                    else 0 end) score
            from Matches
        )
        union all
        (
            select guest_team team, (case when host_goals > guest_goals then 0
                                    when host_goals = guest_goals then 1
                                    else 3 end) score
            from Matches
        )
    ) temp
    group by team
) Scores
on Teams.team_id = Scores.team
order by num_points desc, team_id

690 ms

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 题目
  • 2. 解题
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档