我有两个表学生和考试:学生表:
Id Name
1 Samantha
2 Jane
3 Bob
4 Scarlet
5 David
考试:
student_id subject
1 Biology
1 physics
3 history
4 geography
4 geography
现在我需要找出哪个学生在哪一次考试中出现了多少次,我期望的确切答案是:
student.id examination.subject no_of_times
1 biology 1
1 physics 1
3 history 1
4 geography 2
我尝试了下面的查询,但得到了错误的答案:
select Student.id, examination.subject, count(*) from Student join examination Student.id = examination.student_id;
请帮助我编写正确的查询以获得正确的预期输出!!
谢谢!
发布于 2021-01-12 14:18:13
对于您的精确查询,我们甚至不需要连接,我们可以独占使用第二个Examination
表:
SELECT student_id, subject, COUNT(*) AS no_of_times
FROM Examination
GROUP BY student_id, subject;
https://stackoverflow.com/questions/65678849
复制相似问题