MySQL高阶1783-大满贯数量

题目

找出每一个球员赢得大满贯比赛的次数。结果不包含没有赢得比赛的球员的ID 。

结果集 无顺序要求

准备数据

python 复制代码
Create table If Not Exists Players (player_id int, player_name varchar(20));
Create table If Not Exists Championships (year int, Wimbledon int, Fr_open int, US_open int, Au_open int);
Truncate table Players;
insert into Players (player_id, player_name) values ('1', 'Nadal');
insert into Players (player_id, player_name) values ('2', 'Federer');
insert into Players (player_id, player_name) values ('3', 'Novak');
Truncate table Championships;
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2018', '1', '1', '1', '1');
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2019', '1', '1', '2', '2');
insert into Championships (year, Wimbledon, Fr_open, US_open, Au_open) values ('2020', '2', '1', '2', '2');
复制代码
Championships表
复制代码
Players表

分析数据

类型是行转列 一般要使用union(all)

第一步:将几行转成一列,使用union all

sql 复制代码
select Wimbledon from Championships
union all
select Fr_open from Championships
union all
select US_open from Championships
union all
select Au_open from Championships;

第二步:将两张表进行关联

sql 复制代码
select player_id,player_name,count(*) as grand_slams_count
from players join
    (select Wimbledon from Championships
      union all
      select Fr_open from Championships
      union all
      select US_open from Championships
      union all
      select Au_open from Championships) t1
    on t1.Wimbledon = player_id
group by player_id, player_name;

总结

相关推荐
做cv的小昊7 分钟前
【TJU】信息检索与分析课程笔记和练习(6)英文数据库检索—web of science
大数据·数据库·笔记·学习·全文检索
warton8831 分钟前
ubuntu24.04 安装mysql8.0.36
linux·运维·mysql
严同学正在努力41 分钟前
VMware安装银河麒麟V10操作系统X86_64全过程
数据库·鸿蒙系统·kylin
PoppyBu44 分钟前
Ubuntu20.04版本上安装最新版本的scrcpy工具
android·ubuntu
智源研究院官方账号1 小时前
众智FlagOS 1.6发布,以统一架构推动AI硬件、软件技术生态创新发展
数据库·人工智能·算法·架构·编辑器·硬件工程·开源软件
执念、坚持1 小时前
Property Service源码分析
android
dishugj1 小时前
[SQLSERVER] Lock Waits/sec参数含义详解
数据库·oracle·sqlserver
我科绝伦(Huanhuan Zhou)1 小时前
Oracle锁等待深度解析:从理论到实战的全方位指南
数据库·oracle
小Mie不吃饭1 小时前
Oracle vs MySQL 全面对比分析
数据库·mysql·oracle
我科绝伦(Huanhuan Zhou)1 小时前
KingbaseES数据库备份与恢复深度解析:原理、策略与实践
数据库·金仓数据库