MySQL Anti Join

user3477008 picture user3477008 · Apr 13, 2014 · Viewed 8.7k times · Source

I have two Tables:

Course: Id, Name

Teacher: IdUser, IdCourse, IdSchool

Now, for Example I have a user with the id 10 and a School with the id 4 .

I want to make a Select over all the Cousrses in the table Course, that their Id are NOT recorded in the Table Teacher at the same line with the IdUser 10 and IdSchool 4.

How could I make this query?

Answer

jeojavi picture jeojavi · Apr 13, 2014
SELECT Id, Name
FROM Course
WHERE Id NOT IN (
    SELECT IdCourse 
    FROM Teacher 
    WHERE IdUser = 10 AND IdSchool = 4
);