I have the following tables:
Schools
Teachers
Courses
XRefSchoolsTeachers
XRefCoursesTeachers
Now I am trying to get the list of all the teachers at the college the course is offered at, which are NOT teaching that course. So far I have:
SELECT *
FROM Courses
INNER JOIN XRefSchoolsTeachers
ON Courses.SchoolID = XRefSchoolsTeachers.SchoolID
which gives me the IDs of all the teachers at the school which the course is offered at, but now I need to remove all the IDs of teachers which are not against the ID of that course in XRefCoursesTeachers
. I looked at another question here about how to remove things not on another list, and I need a left join for that. But I feel like I need an inner join too so that I only get teachers from that school which are not on the list already ><
e.g.
SELECT *
FROM Courses
INNER JOIN XRefSchoolsTeachers
ON Courses.SchoolID = XRefSchoolsTeachers.SchoolID
LEFT JOIN XRefCoursesTeachers
ON (XRefCoursesTeachers.TeacherID = XRefSchoolsTeachers.TeacherID
AND XRefCoursesTeachers.CourseID = Courses.ID)
WHERE Courses.ID = ? AND XRefSchoolsTeachers.TeacherID IS NULL
will give me all the teachers at any other course at any other school - not what I want. I want all the teachers which could teach this course but are not.
It is like I need an inner join on the XRefCourseTeachers.CourseID = Courses.ID
, and then a left join on the same instance of XRefCourseTeachers
using TeacherID
so that I can pick out the NULL
results. But I don't get how to do this.
How is this possible? Am I taking the correct approach?