mysql选择多个具有所有条件的多个

时间:2017-08-07 10:21:13

标签: mysql many-to-many

看到这张图片:

如何选择dorm_id(70)?

dorm_id是家庭ides,设施是主题的属性,每个家庭都有一些设施,例如家庭id 70有这些设施(12,13,14,17,18),如何从dorm_id列中选择70号

它是一个搜索查询,可以找到具有所有功能的dorm_id

提前致谢

2 个答案:

答案 0 :(得分:1)

如果您将facility_id放在名为table_facility的表中,则可以这样做:

select dorm_id 
from table_dorm
group by dorm_id
having count(distinct facility_id)=(select count(distinct facility_id)
                                    from table_facility)

如果您将它们放在同一个表中,则可以使用此查询执行此操作:

select dorm_id 
from table_dorm
group by dorm_id
having count(distinct facility_id)=(select count(distinct facility_id)
                                    from table_dorm)

答案 1 :(得分:0)

我解决了这个问题,你需要的只是你没有仔细看到这张照片!

SELECT dorm_id
FROM dorm
WHERE facility_id IN (12, 13, 14)
GROUP BY doctor_id
HAVING COUNT(DISTINCT ability_id) = 3