我有一个student_class表,用于将学生与班级进行映射。我希望从这个表中获取student_id,并将类ID作为输入。 e.g:
现在我想要一个mysql查询,我将在其中传递class_ids 3,5和9,它应该返回我student_id 1。???
答案 0 :(得分:1)
您可以使用having
执行此操作:
select student_id
from student_class
where class_id in (3, 5, 9)
group by student_id
having count(distinct class_id) = 3
答案 1 :(得分:0)
这是另一种特定于mysql的方法
select student_id
from student_class
where class_id in (3, 5, 9)
group by student_id
having sum(class_id = 3) > 0
and sum(class_id = 5) > 0
and sum(class_id = 9) > 0