sql >> Database teknologi >  >> RDS >> Mysql

MySQL:Få produkter med alle attributter på listen

-- 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;

se sqlfiddle her
se også dette svar



  1. Bedste fremgangsmåder til at gemme kreditkortoplysninger i databasen

  2. find fortløbende transaktion inden for 10 minutter

  3. Indsættelse af afkrydsningsfeltværdier i databasen

  4. Kald til et medlem funktion result() ved hjælp af CodeIgniter