我有一个包含以下表格的数据库
Courses: CourseID*, CourseName, TeacherID
Teachers: TeacherID*, TeacherName
Students: StudentID*, StudentName
StudentCourses: CourseID*, StudentID*
图例: *是主键。
如何写出产生的查询产生每位老师教授的学生人数?
例如
TeacherName, Count
Bob 15
Sarah 5
Zubair 1
编辑
select "TeacherName", count(*)
from courses inner join teachers on courses."TeacherID" = teachers."TeacherI"
join sutdentcourses on sutdentcourses."CourseID" = courses."CourseID"
group by "TeacherName"
order by "TeacherName";
答案 0 :(得分:2)
您需要在教师,课程和学生课程表之间建立联接,按教师名称对记录进行分组并计算学生ID。
SELECT TeacherName, COUNT(StudentID)
FROM Teachers JOIN Courses USING(TeacherID)
JOIN StudentCourses USING(CourseID)
GROUP BY TeacherName