Ai
1 Star 0 Fork 0

逸扬/leetcode-hub-mysql

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lc-1212.test 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
# $1212. 查询球队积分
# https://leetcode-cn.com/problems/team-scores-in-football-tournament/
# SQL架构
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');
# Write your MySQL query statement below
select
t.team_id,
t.team_name,
ifnull(score, 0) as num_points
from
(
select
team_id,
sum(score) as score
from
(
select
host_team as team_id,
sum(
case
when host_goals > guest_goals then 3
when host_goals < guest_goals then 0
else 1
end
) as score
from
Matches
group by
host_team
union
all
select
guest_team as team_id,
sum(
case
when host_goals > guest_goals then 0
when host_goals < guest_goals then 3
else 1
end
) as score
from
Matches
group by
guest_team
) tmp1
group by
team_id
) tmp2
right join Teams t on t.team_id = tmp2.team_id
order by
num_points desc,
t.team_id;
# clean-up
drop table Teams;
drop table Matches;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gdut_yy/leetcode-hub-mysql.git
git@gitee.com:gdut_yy/leetcode-hub-mysql.git
gdut_yy
leetcode-hub-mysql
leetcode-hub-mysql
master

搜索帮助