I have 3 mysql table subjects, exams and exams_subjects. subject contains subject ids, exam contains exam ids and exam_subjects contains subjects ids and exam ids. I want to write a mysql query to get an output of subject ids from exams_subjects table that are common to all exam ids. ie the intersection of subject ids for different exam ids. How can I write such a query ?
Asked
Active
Viewed 2,083 times
1 Answers
0
SELECT subjectid
FROM exam_subjects
WHERE examid IN (SELECT examid FROM exams)
GROUP BY subjectid
HAVING COUNT(*) = (SELECT COUNT(examid) FROM exams);
I got this with the help of a similar question in stack overflow How to do intersection on a composition table