Query that checks for subset to mysql query SELECT PA.PATIENT_ID, PA.FIRSTNAME, PA.LASTNAME, PH.* 
FROM pharmacies as PH, patients as PA 
WHERE 
(SELECT D.DRUG_ID 
FROM drugs as D, sells as S 
WHERE D.DRUG_ID = S.DRUG_ID AND PH.PHARMACY_ID = S.PHARMACY_ID and PA.TOWN = PH.TOWN ) 
contains 
(SELECT D.DRUG_ID 
FROM prescriptions as PR, drugs as D 
WHERE PR.PATIENT_ID = PA.PATIENT_ID AND PR.DRUG_ID = D.DRUG_ID)


I have the above query and I would like to write it for Mysql. I want to find for all the patients, the pharmacies that are in the same town, and contain all their drugs.
That is a challenge!

By comparing now many drugs a patient needs to how many a pharmacy can provide of what he needs, I think this gives a list of patient:pharmacy matches.

SELECT  have.patient_id,  have.pharmacy_id
    FROM ( SELECT s.pharmacy_id, pr.patient_id, COUNT(*) AS ct
            FROM sells AS s
            JOIN prescriptions AS pr  USING(drug_id)
            GROUP BY  s.pharmacy_id, pr.patient_id ) AS have
    JOIN ( SELECT patient_id, COUNT(*) AS ct
            FROM prescriptions
            GROUP BY patient_id ) AS need
        ON need.patient_id = have.patient_id
    WHERE need.ct = have.ct


That can then be used as a derived table to get the other specifics:

SELECT PA.PATIENT_ID, PA.FIRSTNAME, PA.LASTNAME, PH.* 
    FROM ( the-above-query ) AS x
    JOIN pharmacies as PH  USING(pharmacy_id)
    JOIN patients as PA    USING(patient_id)


Another approach might involve using LEFT JOIN to discover which pharmacies cannot supply a needed drug for a given patient, then use another LEFT JOIN to discover which other pharmacies there are for that patient.