首页 > 其他分享 >1280. 学生们参加各科测试的次数

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

时间:2023-04-28 11:15:51浏览次数:33  
标签:1280 测试 subject 次数 student 各科 id Math name

【题目】

学生表: 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/problems/students-and-examinations
【思路】

学生表 右连接测试表然后内连接课程表 匹配学生表中id与测试表id相同的项(左连接保留null),内联课程表和测试表中课程相同的项。

count 参加测试课程的数量

【代码】

# Write your MySQL query statement below
SELECT s1.student_id,s1.student_name,s2.subject_name,COUNT(e.subject_name) attended_exams
FROM Students s1
JOIN Subjects s2
LEFT JOIN Examinations e
ON s1.student_id = e.student_id and s2.subject_name = e.subject_name
GROUP BY s1.student_id,s2.subject_name
ORDER BY s1.student_id,s2.subject_name;

 

标签:1280,测试,subject,次数,student,各科,id,Math,name
From: https://www.cnblogs.com/End1ess/p/17361529.html

相关文章

  • hdoj 前m大的数 1280 (技巧&暴力)
    前m大的数TimeLimit:2000/1000MS(Java/Others)   MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):14345   AcceptedSubmission(s):4888ProblemDescription还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的......
  • 通过Netscaler设置用户云桌面密码输入次数
    登录Netscaler,进入Configuration界面选择根据如图所示顺序,点击VirtualServers3.编辑BasicSettings4.选择“more”下拉菜单5.找到如图所示配置选项,自定义用户最大输错密码次数以及锁定时间6.配置完成后点击“ok”保存配置7.登录到外网云桌面,进行测试。如图所示由于输错密码达到设......
  • [LeetCode] 1342. Number of Steps to Reduce a Number to Zero 将数字变成 0 的操作
    Givenaninteger num,return thenumberofstepstoreduceittozero.Inonestep,ifthecurrentnumberiseven,youhavetodivideitby 2,otherwise,youhavetosubtract 1 fromit.Example1:Input:num=14Output:6Explanation: Step1)14ise......
  • 查找指定字符串在某个字符串中的出现次数
    c语言代码实现:此处)折叠或打开1.#include<stdio.h>2.<string.h>3.intsearchnum(char*str,char*pattern)4.{5.if(str==NULL)6.;7.*pos=NULL;8.intcount=0;9.while((pos=strstr(str,pattern))!=NULL)10.{11.++;1......
  • m基于BP译码算法的QC-LDPC误码率matlab仿真,对比不同译码迭代次数的误码率性能
    1.算法仿真效果matlab2022a仿真结果如下: 2.算法涉及理论知识概要       LDPC码是麻省理工学院RobertGallager于1963年在博士论文中提出的一种具有稀疏校验矩阵的分组纠错码。几乎适用于所有的信道,因此成为编码界近年来的研究热点。它的性能逼近香农极限,且描述和实现......
  • m基于BP译码算法的QC-LDPC误码率matlab仿真,对比不同译码迭代次数的误码率性能
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要LDPC码是麻省理工学院RobertGallager于1963年在博士论文中提出的一种具有稀疏校验矩阵的分组纠错码。几乎适用于所有的信道,因此成为编码界近年来的研究热点。它的性能逼近香农极限,且描述和实现简单,易于进行理论分......
  • 牛客网——数组中出现次数超过一半的数字
    title:牛客网——数组中出现次数超过一半的数字题目描述:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。示例:输入[1,2,3,2,2,2......
  • 从 1 到 n 整数中 1 出现的次数
    classSolution{public:intnumberOf1Between1AndN_Solution(intn){vector<int>q;do{q.push_back(n%10);n/=10;}while(n);intres=0;for(inti=q.size()-1;i>=0;......
  • m规则LDPC和非规则LDPC误码率matlab对比仿真,并对比不同译码迭代次数的误码率
    1.算法仿真效果matlab2022a仿真结果如下:2.算法涉及理论知识概要LDPC码是麻省理工学院RobertGallager于1963年在博士论文中提出的一种具有稀疏校验矩阵的分组纠错码。几乎适用于所有的信道,因此成为编码界近年来的研究热点。它的性能逼近香农极限,且描述和实现简单,易于进行理论......
  • API 调用次数限制实现
    在开发接口服务器的过程中,为了防止客户端对于接口的滥用,保护服务器的资源,通常来说我们会对于服务器上的各种接口进行调用次数的限制。比如对于某个用户,他在一个时间段(interval)内,比如1分钟,调用服务器接口的次数不能够大于一个上限(limit),比如说100次。如果用户调用接口的次数......