查询球队积分
表: Teams
+---------------+----------+
| Column Name | Type |
+---------------+----------+
| team_id | int |
| team_name | varchar |
+---------------+----------+
team_id 是该表具有唯一值的列。
表中的每一行都代表一支独立足球队。
表: Matches
+---------------+---------+
| 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 分别表示。
你希望在所有比赛之后计算所有球队的比分。积分奖励方式如下:
- 如果球队赢了比赛(即比对手进更多的球),就得 3 分。
- 如果双方打成平手(即,与对方得分相同),则得 1 分。
- 如果球队输掉了比赛(例如,比对手少进球),就 不得分 。
编写解决方案,以找出每个队的 team_id
,team_name
和 num_points
。
返回的结果根据 num_points
降序排序,如果有两队积分相同,那么这两队按 team_id
升序排序。
返回结果格式如下。
示例 1:
输入:
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 |
+------------+--------------+---------------+-------------+--------------+
输出:
+------------+--------------+---------------+
| 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 |
+------------+--------------+---------------+
数据准备
drop database if exists db_1;
create database db_1;
use db_1;;
Create table If Not Exists Teams (team_id int, team_name varchar(30));
Create table If Not Exists Matches (match_id int, host_team int, guest_team int, host_goals int, guest_goals int);
Truncate table Teams;
insert into Teams (team_id, team_name) values ('10', 'Leetcode FC');
insert into Teams (team_id, team_name) values ('20', 'NewYork FC');
insert into Teams (team_id, team_name) values ('30', 'Atlanta FC');
insert into Teams (team_id, team_name) values ('40', 'Chicago FC');
insert into Teams (team_id, team_name) values ('50', 'Toronto FC');
Truncate table Matches;
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('1', '10', '20', '3', '0');
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('2', '30', '10', '2', '2');
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('3', '10', '50', '5', '1');
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('4', '20', '30', '1', '0');
insert into Matches (match_id, host_team, guest_team, host_goals, guest_goals) values ('5', '50', '30', '1', '0');
teams 表
Matches 表
分析 + 实现
第一步:计算每一个球队作为主队的得分
-- 计算每一个球队作为主队的得分
select
host_team team_id,
case
when host_goals > guest_goals then 3
when host_goals = guest_goals then 1
else 0
end as num_points
from matches;
第二步:计算每一个球队作为客队的得分
-- 计算每一个球队作为客队的得分
select
guest_team team_id,
case
when guest_goals > host_goals then 3
when guest_goals = host_goals then 1
else 0
end as num_points
from matches;
第三步:组建临时表
-- 计算每一个球队作为主队的得分
select
host_team team_id,
case
when host_goals > guest_goals then 3
when host_goals = guest_goals then 1
else 0
end as num_points
from matches
union all
-- 计算每一个球队作为客队的得分
select
guest_team team_id,
case
when guest_goals > host_goals then 3
when guest_goals = host_goals then 1
else 0
end as num_points
from matches
第四步:与 teams 表
进行左连接,查询每一个球队的分数
select
*
from teams t
left join (
-- 计算每一个球队作为主队的得分
select
host_team team_id,
case
when host_goals > guest_goals then 3
when host_goals = guest_goals then 1
else 0
end as num_points
from matches
union all
-- 计算每一个球队作为客队的得分
select
guest_team team_id,
case
when guest_goals > host_goals then 3
when guest_goals = host_goals then 1
else 0
end as num_points
from matches
) points on t.team_id = points.team_id;
第五步:分组聚合,求总分并排序
select
t.team_id,
t.team_name,
ifnull(sum(points.num_points), 0) as num_points
from teams t
left join (
-- 计算每一个球队作为主队的得分
select
host_team team_id,
case
when host_goals > guest_goals then 3
when host_goals = guest_goals then 1
else 0
end as num_points
from matches
union all
-- 计算每一个球队作为客队的得分
select
guest_team team_id,
case
when guest_goals > host_goals then 3
when guest_goals = host_goals then 1
else 0
end as num_points
from matches
) points on t.team_id = points.team_id
group by t.team_id, t.team_name
order by num_points desc, t.team_id;
总结
多表联查
case when 语句
分组聚合
排序