当前位置: 首页 > article >正文

MySQL高阶1783-大满贯数量

题目

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

结果集 无顺序要求 。

准备数据

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

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;

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

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;

总结

  • 若是行转列,使用union all
  • 若是列转行, 利用if函数将数据拉宽

http://www.kler.cn/news/309956.html

相关文章:

  • 使用python 将world的题库导入某学习软件的模板
  • 【RabbitMQ】工作模式
  • 20240911软考架构-------软考156-160答案解析
  • 【物联网】深入解析时序数据库TDengine及其Java应用实践
  • 力扣最热一百题——合并两个有序链表
  • do { ... } while (0) 的意义
  • 单片机拍照_将采集的RGB图像封装为BMP格式保存到SD卡
  • Pandas_sqlite
  • bestphp‘s revenge1
  • 排序----数据结构
  • 9.18日常记录
  • cmd修改游戏数据处理量大小
  • vue获取最近7天时间;获取任意时间段时间
  • 【前端】main.js中app.vue中 render函数的作用及使用背景
  • 【数据库】MySQL-基础篇-事务
  • 架构设计——概念和基础
  • 在 Android 中,自定义 View 的绘制流程
  • 【原创】java+springboot+mysql校园订餐网系统设计与实现
  • JSON语法
  • go语言后端开发学习(七)——如何在gin框架中集成限流中间件
  • 【VUE】快速上手
  • 企业CAD图纸防泄密措施有哪些?10个真实有效方法分享
  • Science Robotics 在小动物模型中实现渐进和可逆主动脉收缩的软机器人平台
  • 202409011在飞凌的OK3588-C的核心板跑Rockchip原厂的Android12时挂载触摸屏ft5x06之后使用i2c-tools检测
  • 「DAOI R1」Magic
  • gitee远程仓库OPEN GIT BASH HERE从错误中学习
  • 形式向好、成本较低、可拓展性较高的名厨亮灶开源了
  • 打通最后一公里:使用CDN加速GitHub Page的访问
  • 分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
  • No module named MYSQLdb 问题解决