2

所以我正在学习 MySQL,我正在尝试执行以下操作:

对于每位讲师,请列出他/她的姓名和他/她指导的学生人数。

架构的相关部分是:

Person(ID, Name)
Student(StudentID, MentorID)
Instructor(InstructorID)

InstructorIDStudentID映射到Person.IDMentorID映射到(每个学生都有InstructorID一个导师导师,导师和学生都是人)。

我尝试了以下方法无济于事:

select p.Name, count(select s.StudentID
                     from Student s
                     where s.MentorID = i.InstructorID)
from Person p, Instructor i
where p.ID = i.InstructorID;

在阅读 StackOverflow 上的一些内容后,这也是:

select InstructorDetails.Name, count(Mentees)
from Instructor i
inner join Person as InstructorDetails
    on InstructorDetails.ID = i.InstructorID
inner join Student as Mentees
    on Mentees.MentorID = i.InstructorID;

有什么建议么?

4

1 回答 1

3

你缺乏GROUP BY你的查询,

SELECT InstructorDetails.Name, count(*) totalCount
FROM   Instructor i
       INNER JOIN Person as InstructorDetails
           ON InstructorDetails.ID = i.InstructorID
       INNER JOIN Student as Mentees
           ON Mentees.MentorID = i.InstructorID
GROUP BY InstructorDetails.Name
于 2012-09-19T03:55:34.693 回答