mysql select many to many that have all condition

人走茶凉 提交于 2019-12-12 04:37:03

问题


just see this picture :

how to I select 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 .

its a search query that find dorm_id that has all facility

thanks in advance


回答1:


You can do it this way if you have your facility_id in a table called 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)

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)



回答2:


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


来源:https://stackoverflow.com/questions/45544626/mysql-select-many-to-many-that-have-all-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!