简体   繁体   English

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

[英]mysql select many to many that have all condition

just see this picture : 只是看这张图片:

how to I select dorm_id(70)? 如何选择dorm_id(70)?

dorm_id is home ides , and facility is the propertray of theme , each home has some facility for example home id 70 has these facilities (12,13,14,17,18) , how to I select number 70 from dorm_id column . dorm_id是家庭的想法,而设施是主题的属性,每个家庭都有一些设施,例如home id 70有这些设施(12,13,14,17,18),我如何从dorm_id列中选择数字70。

its a search query that find dorm_id that has all facility 其搜索查询查找具有所有功能的dorm_id

thanks in advance 提前致谢

You can do it this way if you have your facility_id in a table called table_facility: 如果您在名为table_facility的表中包含了facility_id,则可以采用这种方式:

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

If you have them in the same table you can do it with this query: 如果您将它们放在同一张表中,则可以使用以下查询进行操作:

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

I solve the problem , all you need is you dont see carefully the picture ! 我解决了问题,您所需要的就是您不仔细看图片!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM