gpt4 book ai didi

mysql - SQL查询: count the number of subjects enrolled by each student

转载 作者:行者123 更新时间:2023-11-29 09:19:45 25 4
gpt4 key购买 nike

我有两个表:科目和学生。我正在尝试计算每个学生注册的科目数。我怎么做?我正在尝试下面的代码,但它没有给出我需要的答案。

SELECT COUNT( subject.SUBJECT ) , student.IDNO, student.FIRSTNAME, subject.SUBJECT
FROM student, subject
GROUP BY subject.SUBJECT
LIMIT 0 , 30

最佳答案

你也有一个招生表,不是吗?解决student和subject之间的多对多关系。你可以用它来工作。假设您的注册表有 StudentID 和 subjectID 列,那么您只需要:

SELECT studentID, COUNT(*) FROM Enrolment GROUP BY studentId;

或者,也包括他们的名字,

SELECT s.firstname, COUNT(*) FROM Enrolment e JOIN student s on e.studentId=s.IDNO GROUP BY e.studentId;

关于mysql - SQL查询: count the number of subjects enrolled by each student,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2483253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com