MySQL Merging my selects together MySQL Merging my selects together sql sql

MySQL Merging my selects together


This is called Relational Division.

SELECT id_roomFROM services_roomsWHERE id_service IN (1,2,3)GROUP BY id_roomHAVING COUNT(*) = 3

if unique constraint was not enforced on id_service for ech id_room, DISTINCT is required.

SELECT id_roomFROM services_roomsWHERE id_service IN (1,2,3)GROUP BY id_roomHAVING COUNT(DISTINCT id_service) = 3


The answer to your question returns all rooms that have at least one of the services. The query is:

SELECT id_room, count(*) as NumServicesFROM services_roomsWHERE id_service IN (1,2,3)GROUP BY id_roomHAVING COUNT(*) > 0order by count(*) desc