There is pretty good article how to filter results in a has-many relation: How to filter SQL results in a has-many-through relation
I'm just seeking a solution for COUNT result, not show them all.
student {
id
name
}
club {
id
name
}
student_club {
student_id
club_id
}
How many students are in both CLUB1 && CLUB2?
EDIT: It would be great to use "Martin 2" method from a link below:
SELECT s.stud_id, s.name
FROM student s
JOIN student_club sc USING (stud_id)
WHERE sc.club_id IN (30, 50)
GROUP BY 1,2
HAVING COUNT(*) > 1;
Just adding something to COUNT results.