leetcode----mysql

1280. 学生们参加各科测试的次数 - 力扣(LeetCode)

学生表: Students

复制代码
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student_id    | int     |
| student_name  | varchar |
+---------------+---------+
在 SQL 中,主键为 student_id(学生ID)。
该表内的每一行都记录有学校一名学生的信息。

科目表: Subjects

复制代码
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| subject_name | varchar |
+--------------+---------+
在 SQL 中,主键为 subject_name(科目名称)。
每一行记录学校的一门科目名称。

考试表: Examinations

复制代码
+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| student_id   | int     |
| subject_name | varchar |
+--------------+---------+
这个表可能包含重复数据(换句话说,在 SQL 中,这个表没有主键)。
学生表里的一个学生修读科目表里的每一门科目。
这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。

查询出每个学生参加每一门科目测试的次数,结果按 student_idsubject_name 排序。

查询结构格式如下所示。

示例 1:

复制代码
输入:
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         |
+------------+--------------+
输出:
+------------+--------------+--------------+----------------+
| 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 次。

Create table If Not Exists Students (student_id int, student_name varchar(20))

Create table If Not Exists Subjects (subject_name varchar(20))

Create table If Not Exists Examinations (student_id int, subject_name varchar(20))

Truncate table Students

insert into Students (student_id, student_name) values ('1', 'Alice')

insert into Students (student_id, student_name) values ('2', 'Bob')

insert into Students (student_id, student_name) values ('13', 'John')

insert into Students (student_id, student_name) values ('6', 'Alex')

Truncate table Subjects

insert into Subjects (subject_name) values ('Math')

insert into Subjects (subject_name) values ('Physics')

insert into Subjects (subject_name) values ('Programming')

Truncate table Examinations

insert into Examinations (student_id, subject_name) values ('1', 'Math')

insert into Examinations (student_id, subject_name) values ('1', 'Physics')

insert into Examinations (student_id, subject_name) values ('1', 'Programming')

insert into Examinations (student_id, subject_name) values ('2', 'Programming')

insert into Examinations (student_id, subject_name) values ('1', 'Physics')

insert into Examinations (student_id, subject_name) values ('1', 'Math')

insert into Examinations (student_id, subject_name) values ('13', 'Math')

insert into Examinations (student_id, subject_name) values ('13', 'Programming')

insert into Examinations (student_id, subject_name) values ('13', 'Physics')

insert into Examinations (student_id, subject_name) values ('2', 'Math')

insert into Examinations (student_id, subject_name) values ('1', 'Math')

SELECT

s.student_id,

s.student_name,

sub.subject_name,

IFNULL(grouped.attended_exams, 0) AS attended_exams

FROM

Students s

CROSS JOIN

Subjects sub

LEFT JOIN (

SELECT student_id, subject_name, COUNT(*) AS attended_exams

FROM Examinations

GROUP BY student_id, subject_name

) grouped

ON s.student_id = grouped.student_id AND sub.subject_name = grouped.subject_name

ORDER BY s.student_id, sub.subject_name;

相关推荐
Blossom.11818 分钟前
量子计算在密码学中的应用与挑战:重塑信息安全的未来
人工智能·深度学习·物联网·算法·密码学·量子计算·量子安全
1白天的黑夜122 分钟前
贪心算法-860.柠檬水找零-力扣(LeetCode)
c++·算法·leetcode·贪心算法
搏博30 分钟前
专家系统的基本概念解析——基于《人工智能原理与方法》的深度拓展
人工智能·python·深度学习·算法·机器学习·概率论
yzx99101331 分钟前
决策树随机深林
人工智能·python·算法·决策树·机器学习
Y1nhl1 小时前
力扣hot100_子串_python版本
开发语言·python·算法·leetcode·职场和发展
uhakadotcom1 小时前
过来人给1-3 年技术新人的几点小小的建议,帮助你提升职场竞争力
算法·面试·架构
wuqingshun3141592 小时前
蓝桥杯 16. 密文搜索
c++·算法·职场和发展·蓝桥杯·深度优先
Brookty2 小时前
【数据结构】哈希表
数据结构·算法·哈希算法·散列表
Dovis(誓平步青云)3 小时前
【数据结构】·励志大厂版(复习+刷题):二叉树
c语言·数据结构·经验分享·笔记·学习·算法·学习方法
越城3 小时前
算法效率的钥匙:从大O看复杂度计算 —— C语言数据结构第一讲
c语言·开发语言·数据结构·算法