I can't figure this out so far, I have these tables:
- students - Columns: id, name
- stud_class - Columns: students_id, class_id
- class - Columns: id, courses_id
- courses - Columns: id, name
I need to select the students that don't have a matching row in the stud_class table OR those who aren't in a class of a given course (the given course is a parameter).
For example: select the students that don't have any classes or those that are in a class, but not the class of the course.id = 2
.
I did this so far, but it doesn't work, the query returns no rows. I know it's kind of confusing, but I don't know how to use the JOINS in this case.
SELECT students.id, students.name
FROM students, stud_class, class
WHERE ( NOT (students.id = stud_class.students_id) )
OR ( (students.id=stud_class.students.id) AND
(stud_class.class_id=class.id) AND
( NOT (class.course_id=2) )
);