De 2 filters moeten afzonderlijk worden behandeld, aangezien elk filter naar totaal verschillende records in de products_attributes_values
moet kijken. tafel.
SELECT * FROM products
WHERE category_id IN ('6', '7', '29', '8', '9', '36')
AND id IN (
SELECT product_id
FROM products_attributes_values
WHERE attribute_id = 2
AND value_id = '4')
AND id IN (
SELECT product_id
FROM products_attributes_values
WHERE attribute_id = 3
AND value_id = '11')
Van UAMoto:
Het werkt, maar een beetje traag, dus ik heb join-aliassen gebruikt zoals een persoon me vertelde:
"SELECT * FROM `products`
JOIN `products_attributes_values` AS pav1
ON (`pav1`.`product_id` = `products`.`id`)
JOIN `products_attributes_values` AS pav2
ON (`pav2`.`product_id` = `products`.`id`)
WHERE `category_id` IN ('6', '7', '29', '8', '9', '36')
AND (`pav1`.`attribute_id` = '2' AND `pav1`.`value_id` = '4')
AND (`pav2`.`attribute_id` = '3' AND `pav2`.`value_id` = '11')"
Geüpdatete zoekopdrachten voor combinaties van kenmerken:
SELECT * FROM products
WHERE category_id IN ('6', '7', '29', '8', '9', '36')
AND id IN (
SELECT product_id
FROM products_attributes_values
WHERE attribute_id = 2
AND value_id IN ('4', '5'))
AND id IN (
SELECT product_id
FROM products_attributes_values
WHERE attribute_id = 3
AND value_id IN ('11', '62'))
of
SELECT *
FROM `products`
JOIN `products_attributes_values` AS pav1 ON (`pav1`.`product_id` = `products`.`id`)
JOIN `products_attributes_values` AS pav2 ON (`pav2`.`product_id` = `products`.`id`)
WHERE `category_id` IN ('6', '7', '29', '8', '9', '36')
AND (`pav1`.`attribute_id` = '2' AND `pav1`.`value_id` IN ('4', '5'))
AND (`pav2`.`attribute_id` = '3' AND `pav2`.`value_id` IN ('11', '62'))