文章目录

  • 1. 题目
  • 2. 解题

1. 题目

学生表: Students

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student_id    | int     |
| student_name  | varchar |
+---------------+---------+

主键为 student_id(学生ID),该表内的每一行都记录有学校一名学生的信息。

科目表: Subjects

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| subject_name | varchar |
+--------------+---------+

主键为 subject_name(科目名称),每一行记录学校的一门科目名称。

考试表: Examinations

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| student_id   | int     |
| subject_name | varchar |
+--------------+---------+

这张表压根没有主键,可能会有重复行。
学生表里的一个学生修读科目表里的每一门科目,
而这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。

要求写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序

查询结构格式如下所示:

Students table:
+------------+--------------+
| student_id | student_name |
+------------+--------------+
| 1          | Alice        |
| 2          | Bob          |
| 13         | John         |
| 6          | Alex         |
+------------+--------------+
Subjects table:
+--------------+
| subject_name |
+--------------+
| Math         |
| Physics      |
| Programming  |
+--------------+
Examinations table:
+------------+--------------+
| student_id | subject_name |
+------------+--------------+
| 1          | Math         |
| 1          | Physics      |
| 1          | Programming  |
| 2          | Programming  |
| 1          | Physics      |
| 1          | Math         |
| 13         | Math         |
| 13         | Programming  |
| 13         | Physics      |
| 2          | Math         |
| 1          | Math         |
+------------+--------------+
Result table:
+------------+--------------+--------------+----------------+
| student_id | student_name | subject_name | attended_exams |
+------------+--------------+--------------+----------------+
| 1          | Alice        | Math         | 3              |
| 1          | Alice        | Physics      | 2              |
| 1          | Alice        | Programming  | 1              |
| 2          | Bob          | Math         | 1              |
| 2          | Bob          | Physics      | 0              |
| 2          | Bob          | Programming  | 1              |
| 6          | Alex         | Math         | 0              |
| 6          | Alex         | Physics      | 0              |
| 6          | Alex         | Programming  | 0              |
| 13         | John         | Math         | 1              |
| 13         | John         | Physics      | 1              |
| 13         | John         | Programming  | 1              |
+------------+--------------+--------------+----------------+
结果表需包含所有学生和所有科目(即便测试次数为0):
Alice 参加了 3 次数学测试, 2 次物理测试,以及 1 次编程测试;
Bob 参加了 1 次数学测试, 1 次编程测试,没有参加物理测试;
Alex 啥测试都没参加;
John  参加了数学、物理、编程测试各 1 次。

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

2. 解题

  • 先进行内连接产生所有的组合
select *
from Students st
join Subjects sub{"headers": ["student_id", "student_name", "subject_name"], "values":
[[1, "Alice", "Programming"], [1, "Alice", "Physics"], [1, "Alice", "Math"], [2, "Bob", "Programming"], [2, "Bob", "Physics"], [2, "Bob", "Math"], [13, "John", "Programming"], [13, "John", "Physics"], [13, "John", "Math"], [6, "Alex", "Programming"], [6, "Alex", "Physics"], [6, "Alex", "Math"]]}
  • 再把考试表左连接于上表
{"headers": ["student_id", "student_name", "subject_name", "student_id", "subject_name"], "values": [[1, "Alice", "Programming", 1, "Programming"], [1, "Alice", "Physics", 1, "Physics"], [1, "Alice", "Physics", 1, "Physics"], [1, "Alice", "Math", 1, "Math"], [1, "Alice", "Math", 1, "Math"], [1, "Alice", "Math", 1, "Math"], [2, "Bob", "Programming", 2, "Programming"], [2, "Bob", "Physics", null, null], [2, "Bob", "Math", 2, "Math"], [13, "John", "Programming", 13, "Programming"], [13, "John", "Physics", 13, "Physics"], [13, "John", "Math", 13, "Math"], [6, "Alex", "Programming", null, null], [6, "Alex", "Physics", null, null], [6, "Alex", "Math", null, null]]}
  • 再分组
# Write your MySQL query statement below
select t.student_id, t.student_name, t.subject_name,sum(if(e.subject_name is null, 0, 1)) attended_exams
from
(select *from Students stjoin Subjects sub
) t
left join Examinations e
on t.student_id = e.student_id and t.subject_name = e.subject_name
group by t.student_id, t.subject_name
order by t.student_id, t.subject_name

720 ms

或者用 count(e.subject_name) attended_exams,null 项不会被计算,返回不为null


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!

LeetCode MySQL 1280. 学生们参加各科测试的次数相关推荐

  1. LeetCode:Database 76.学生们参加各科测试的次数

    要求:写一段 SQL 语句,查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序. 学生表: Students的结构 +-------------- ...

  2. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 23丨学生们参加各科测试的次数【难度简单】​

    [未来的你,会感谢今天努力的你]每日两题,一难一易,每天进步一点点,可能会直接导致一场面试的成功,或工作的轻松搞定,从而升职加薪迎娶白富美,加油小伙伴!

  3. LeetCode MySQL 618. 学生地理信息报告(row_number)

    文章目录 1. 题目 2. 解题 1. 题目 一所美国大学有来自亚洲.欧洲和美洲的学生,他们的地理信息存放在如下 student 表中. | name | continent | |--------| ...

  4. LeetCode MySQL解题目录

    已完成的 LeetCode MySQL 数据库题目.点击查看我的 LeetCode 算法解题目录. 已解决 123/123 - 简单 54 中等 51 困难 18 前置入门学习 MySQL 基本查询. ...

  5. mysql输出学生和考试信息_mysql练习题-查询同时参加计算机和英语考试的学生的信息-遁地龙卷风...

    文章参考http://blog.sina.com.cn/willcaty. 针对其中的一道练习题想出两种其他的答案,希望网友给出更多回答. (0) 基础数据 student表 +-----+----- ...

  6. (附源码)mysql+ssm学生选课系统 毕业设计 170920

     摘 要 本论文主要论述了如何使用JAVA语言开发一个学生选课系统,本系统将严格按照软件开发流程进行各个阶段的工作,采用B/S架构,面向对象编程思想进行项目开发.在引言中,作者将论述学生选课系统的当前 ...

  7. ssm+mysql+ssm学生选课系统 毕业设计-附源码170920

    摘 要 本论文主要论述了如何使用JAVA语言开发一个学生选课系统,本系统将严格按照软件开发流程进行各个阶段的工作,采用B/S架构,面向对象编程思想进行项目开发.在引言中,作者将论述学生选课系统的当前背 ...

  8. Eclipse+Java+Swing+Mysql实现学生信息管理系统

    目录 一.系统介绍 1.开发环境 2.技术选型 3.系统功能 4.数据库 二.系统展示 1.注册系统 2.登录系统 3.系统主页面 4.添加学生信息 5.修改学生信息 6.查询学生信息 三.部分代码 ...

  9. 江苏省计算机学业水平测试模拟软件,基于江苏省普通高中物理学业水平测试的学生在线自主模拟测试系统研发...

    摘要: 项目反应理论(Item Response Theory)(简称IRT),相比于经典测量理论(Class Test Theory)(简称CTT),具有样本独立,可反复测量等诸多优势.随着计算机技 ...

最新文章

  1. [python]目录及文件操作
  2. Docker + gunicron + supervisord 部署python应用
  3. javascript --执行上下文,作用域
  4. Linux学习笔记 文件读写小细节
  5. 深度学习(五十五)tensorflow分布式训练
  6. nginx访问本地目录一直不好使_Nginx跳转本地目录容易犯的错误
  7. 小心调用Replicator, While 和 CAG子活动
  8. Duplicate Photos Fixer Pro for Mac用户指南:我可以比较不同时间的照片吗?
  9. 微信公号“架构师之路”学习笔记(五)-数据库扩展性架构设计(水平切分,秒级扩容,平滑迁移,在线表结构变更,一个大数据量多属性高并发的数据库设计等)
  10. 超简单,自己动手写一个二维码生成器
  11. JAVA将多个Pdf合并成一个Pdf
  12. 基于波动率的期权交易策略分析
  13. 怎么把EXCEL内的十六进制数进行两位两位倒序排列
  14. 2021申请邓白氏编码最新操作手册
  15. Android百度地图,定位图标随着方向的改变而改变
  16. BugKu CTF(杂项篇MISC)---哥哥的秘密
  17. 前端vscode必备插件推荐(墙裂推荐)
  18. 基于卡方的独立性检验
  19. 单片机0 10秒计时C语言,基于单片机的秒,分,时的精确计时实现
  20. 阿里云ECS CentOS6.5搭建iRedMail邮件服务器

热门文章

  1. VS2010中 C++创建DLL图解
  2. linux telnet 权限,允许telnet 通过root用户进行访问
  3. java mongo 查询数组_MongoDB查询(数组、内嵌文档)
  4. mysql5.7物理备份_Mysql5.7—运维常用备份方式
  5. framebuffer驱动详解2——framebuffer驱动框架分析
  6. 面向对象与软件工程—团队作业1
  7. 青蛙学Linux—NFS
  8. Kotlin——初级篇(二):变量、常量、注释
  9. 51Nod 1530 稳定方块
  10. file_put_contents()写入数组