So I have this inner join
SELECT IDEvent,events.IDSubject,events.month,events.year
FROM events
INNER JOIN subjects ON events.IDSubject = subjects.IDSubject
INNER JOIN classes ON subjects.IDClass = classes.IDClass
INNER JOIN students ON classes.IDClass = students.IDClass
WHERE events.month=10
AND events.year=2015
AND (students.DNI=DNI OR events.IDSubject='0')`
What I want to do is to select all the events depending on the user IDSubject AND maybe if there is some event with IDSubject=0 also, I can get the ones that are depending from the user, but can't get the ones with IDSubject=0, have anyone got some clue of how to do it correctly?
My database model if anyone needs to see it
解决方案
SELECT IDEvent,events.IDSubject,events.month,events.year
FROM events
INNER JOIN subjects ON events.IDSubject = subjects.IDSubject
INNER JOIN classes ON subjects.IDClass = classes.IDClass
INNER JOIN students ON classes.IDClass = students.IDClass
WHERE events.month=10
AND events.year=2015
AND students.DNI=DNI
UNION SELECT IDEvent, events.IDSubject ,events.month ,events.year
FROM events WHERE IDSubject='0'