1280. 学生们参加各科测试的次数
1280. 学生们参加各科测试的次数
题目:1280. 学生们参加各科测试的次数
表结构:
Students
| Column Name | Type |
|---|---|
| student_id | int |
| student_name | varchar |
student_id是主键。- 该表的每一行都记录有学校一名学生的信息。
Subjects
| Column Name | Type |
|---|---|
| subject_name | varchar |
subject_name是主键。- 每一行记录学校的一门科目名称。
Examinations
| Column Name | Type |
|---|---|
| student_id | int |
| subject_name | varchar |
- 该表没有主键,可能包含重复数据。
- 学生表里的每个学生修读科目表中的每一门科目。
- 这张考试表的每一行记录就表示学生表里的某个学生参加了一次科目表里某门科目的测试。
问题描述:
查询出每个学生参加每一门科目测试的次数,结果按 student_id 和 subject_name 排序。
示例 1:
输入:
Students 表:
| student_id | student_name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 13 | John |
| 6 | Alex |
Subjects 表:
| subject_name |
|---|
| Math |
| Physics |
| Programming |
Examinations 表:
| 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)。
题解
先用 CROSS JOIN 列出所有学生和科目的组合,再按两列左连接考试记录。没参加考试的组合会得到一条右表列为 NULL 的补行,所以要用 COUNT(ex.subject_name),用 COUNT(*) 会误计为一次。真实的重复考试记录应逐行计数。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT st.student_id,
st.student_name,
su.subject_name,
COUNT(ex.subject_name) AS attended_exams
FROM Students st
CROSS JOIN Subjects su
LEFT JOIN Examinations ex
ON st.student_id = ex.student_id
AND su.subject_name = ex.subject_name
GROUP BY st.student_id,
st.student_name,
su.subject_name
ORDER BY st.student_id,
su.subject_name;
本地复现
环境为 MySQL 8.4.8,使用默认 sql_mode(含 ONLY_FULL_GROUP_BY)。以下语句按示例 1 建表并插入数据:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
CREATE TABLE Students (
student_id INT,
student_name VARCHAR(255)
);
INSERT INTO Students (student_id, student_name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(13, 'John'),
(6, 'Alex');
CREATE TABLE Subjects (
subject_name VARCHAR(255)
);
INSERT INTO Subjects (subject_name) VALUES
('Math'),
('Physics'),
('Programming');
CREATE TABLE Examinations (
student_id INT,
subject_name VARCHAR(255)
);
INSERT INTO Examinations (student_id, subject_name) VALUES
(1, 'Math'),
(1, 'Physics'),
(1, 'Programming'),
(2, 'Programming'),
(1, 'Physics'),
(1, 'Math'),
(13, 'Math'),
(13, 'Programming'),
(13, 'Physics'),
(2, 'Math'),
(1, 'Math');
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
+------------+--------------+--------------+----------------+
| 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 |
+------------+--------------+--------------+----------------+
本文由作者按照 CC BY 4.0 进行授权
