sql >> Databáze >  >> RDS >> Mysql

MySQL:Získejte produkty se všemi uvedenými atributy

-- PHP (or any other languaje) parts are hardcoded here!!!!

SELECT p.*, hma.howmuchattr
-- howmuchattr is needed by HAVING clause, 
-- you can omit elsewhere (by surrounding SELECT or by programming languaje)

FROM products AS p 
LEFT JOIN product_attributes AS pa ON pa.product_id = p.id 
LEFT JOIN (
    SELECT product_id, count(*) as howmuchattr
    FROM product_attributes 
    GROUP BY product_id
) as hma on p.id = hma.product_id

WHERE 
pa.attribute_id IN 
(1,3)                    -- this cames from PHP (or any other languaje). Can be (1) for the other case
GROUP BY p.id
HAVING count(*) = howmuchattr;

viz sqlfiddle zde
viz také tuto odpověď



  1. Aktuální datum v klauzuli WHERE MySQL

  2. MySQL Full Text Search Mystery

  3. DBMS_JOB vs DBMS_SCHEDULER

  4. Na jaký sloupec by měl být umístěn seskupený index?