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

文章目录

    • 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阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474921.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【记录】有关parseInt的讨论

问题由来,某群的一个讨论: parseInt(1/0, 19) 18; parseInt的用法: parseInt(string [, radix]) 注意,第一个参数是String类型,当radix未指定的时候,那么默认基地是10。转换规则:1、首先查看位…

LeetCode MySQL 597. 好友申请 I :总体通过率

文章目录1. 题目2. 解题1. 题目 在 Facebook 或者 Twitter 这样的社交应用中,人们经常会发好友申请也会收到其他人的好友申请。现在给如下两个表: 表: friend_request | sender_id | send_to_id |request_date| |-----------|------------…

贷款利润最大化——利用随机森林和逻辑回归

文章目录分析目的一、数据采集1、数据来源2、数据说明二、数据传输三、数据处理1、查看数据2、清理无用特征值3、标签列分析4、清理只单一值的列5、空值处理6、数据类型转换四、数据挖掘1、构建模型2、导入算法五、总结分析目的 本文针对某信贷网站提供的2007-2011年贷款申请人…

ios学习之UITabBar(标签栏)

自定义TabBar的高度:(无论横屏还是竖屏都以竖屏为准,之前就改为横屏的设置出错,找了好久才找到这个问题) tabBar [[UITabBarController alloc] init];// 改变tabBarController高度 tabBar.tabBar.frameCGRectMake(0,…

LeetCode MySQL 512. 游戏玩法分析 II

文章目录1. 题目2. 解题1. 题目 Table: Activity ----------------------- | Column Name | Type | ----------------------- | player_id | int | | device_id | int | | event_date | date | | games_played | int | ----------------------- (…

Gtk-WARNING **: 无法在模块路径中找到主题引擎:“pixmap”(转)

Gtk-WARNING**:无法在模块路径中找到主题引擎:“pixmap”的解决如果终端中提示:(gvim:2353): Gtk-WARNING **: 无法在模块路径中找到主题引擎:“pixmap”解决方法是运行: sudo apt-get install gtk2-engines-pixbuf 转载于:https://www.cnbl…

机器学习算法优缺点对比及选择

文章目录偏差&方差常见算法优缺点朴素贝叶斯Logistic Regression(逻辑回归)线性回归最近邻算法——KNN决策树ID3、C4.5算法CART分类与回归树AdaboostingSVM支持向量机人工神经网络K-Means聚类EM最大期望算法集成算法(AdaBoost算法&#x…

LeetCode MySQL 1076. 项目员工II

文章目录1. 题目2. 解题1. 题目 Table: Project ---------------------- | Column Name | Type | ---------------------- | project_id | int | | employee_id | int | ---------------------- 主键为 (project_id, employee_id)。 employee_id 是员工表 Employ…

CouchDB与Couchbase:区别何在,Membase又将如何?

CouchDB与Couchbase:区别何在,Membase又将如何? 2012-05-23 02:05 682人阅读 评论(1) 收藏 举报 本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2012/05/couchdb-vs-couchbase-memba…

Sklearn.metrics评估方法

文章目录混淆矩阵分类准确率 accuracy精确率Precision召回率 recallF1值Roc曲线、AUC混淆矩阵 混淆矩阵又称误差矩阵,针对预测值和真实值之间的关系,我们可以将样本分为四个部分,分别是: 真正例(True Positive&#x…

HDU 1492 The number of divisors(约数) about Humble Numbers

The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1741 Accepted Submission(s): 852 Problem DescriptionA number whose only prime factors are 2,3,5 or …

LeetCode MySQL 1083. 销售分析 II

文章目录1. 题目2. 解题1. 题目 Table: Product ----------------------- | Column Name | Type | ----------------------- | product_id | int | | product_name | varchar | | unit_price | int | ----------------------- product_id 是这张表的主键Tabl…

常见的几种流失预警模型

建立预警模型的目的是提前识别潜在流失用户,为挽留用户赢得时间。 流失预警模型,不应该只是单一的模型,而应该是一系列模型。预警模型的优劣通常用准确率、召回率来衡量。准确率:预测为流失的用户中,有多少真的流失。召…

WP7 网络请求之WebClient

WebClient运行于UI线程,支持编码方式的设定、支持POST/GET方式提交、不支持同步请求、不支持超时设定。WP7会缓存URL链 接,所以两次请求,尽管网络端数据发生了变化,得到的还会是同样的数据,这点要特别注意,…

Jupyter Notebook安装 nbextensions 插件

安装 nbextensions 插件 一、打开Anaconda Prompt窗口,执行第一个命令,用于安装nbextensions: pip install jupyter_contrib_nbextensions 二、再执行第二个命令,用于安装 javascript and css files jupyter contrib nbextens…

LeetCode MySQL 1084. 销售分析III

文章目录1. 题目2. 解题1. 题目 Table: Product ----------------------- | Column Name | Type | ----------------------- | product_id | int | | product_name | varchar | | unit_price | int | ----------------------- product_id 是这个表的主键Tabl…

15.使用using和try/finally来做资源清理

只有是实现了IDispose接口的类型的调用都应使用using 或者try/finally释放对象。转载于:https://www.cnblogs.com/movemoon/archive/2012/10/24/2736571.html

数据分析常用Python库:数值计算、可视化、机器学习等领域

镜像pip安装 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 豆瓣(douban) http://pypi.douban.com/simple/ 清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 中国科学技术大学 http://pypi.mirrors.ustc.edu.cn…

LeetCode MySQL 1322. 广告效果

文章目录1. 题目2. 解题1. 题目 表: Ads ------------------------ | Column Name | Type | ------------------------ | ad_id | int | | user_id | int | | action | enum | ------------------------ (ad_id, user_id) 是该表的主键…

linux下练习 c++ 容器set、multimset的特性

print.h //print.h#include <iostream>using namespace std;#ifndef print_fun#define print_funtemplate<typename T>///显示序列数据void print(T b,T e,char c ){bool isExitfalse;while (b!e){cout<<*b<<c;isExittrue;}if(isExit) cout<<end…