我正在努力找到我的数据库中的教师数量,这些教师能够教授由名为“Ian Goldman”的教师授课的课程。
以下是涉及的关系的模式:
/*Instructors that can teach the courses that have been taught by Ian Goldman*/
select
InstructorNo
from
AreasOfCourse, AreasOfInstructor,
(/*courses that have been taught be Ian Goldman*/
select DISTINCT CourseNo
from CourseSections
where InstructorNo = (select InstructorNO
from Instructors
where FirstName = 'Ian'
and LastName = 'Goldman')
) AS GoldmansCourses
where
AreasofCourse.CourseNo = GoldmansCourses.CourseNo
and AreasofInstructor.AreaName = AreasOfCourse.AreaName
group by
InstructorNo
having
COUNT(DISTINCT AreasofCourse.CourseNo) = COUNT((GoldmansCourses.CourseNo)
当我查询数据库(即Goldman教授的课程)时,GoldmansCourses
子查询返回与2个不同课程的关系。当我在最后一行打电话给COUNT((GoldmansCourses.CourseNo))
时,我期待得到2回。
然而,实际发生的是我在每个分组中给出了元组的数量(即查询在from子句中给出了关于笛卡尔积的信息,而不是GoldmansCourses
关系中的课程数量)。
我该如何解决这个问题?我需要访问原始子查询GoldmansCourses
,而不是FROM
子句计算的笛卡尔积中的列。
答案 0 :(得分:0)
SELECT InstructorNo
FROM AreasOfInstructor AI
JOIN AreasOfCourse AC
ON AI.Course = AC.Course
LEFT JOIN GoldmansCourses GC
ON AI.Course = GC.Course
WHERE
GC.Course IS NOT NULL
GROUP BY InstructorNo
HAVING COUNT(DISTINCT AreasofCourse.CourseNo) = COUNT((GoldmansCourses.CourseNo)