Getting products with desired attributes

蓝咒 提交于 2019-12-12 05:48:15

问题


I have a category with products and a set of attributes. Those attributes are "Shape" and "Diameter".

When I open a category without applying filters I get products with following query:

"SELECT * FROM `products` 
WHERE `category_id` 
IN ('6', '7', '29', '8', '9', '36')"

Then I click on attribute "Diameter"(id = 2) with value "8mm"(id = 4) I generate the following query:

"SELECT * FROM `products`
JOIN `products_attributes_values`
ON (`products_attributes_values`.`product_id` = `products`.`id`)
WHERE `category_id` IN ('6', '7', '29', '8', '9', '36')
AND `products_attributes_values`.`attribute_id` = 2
AND `products_attributes_values`.`value_id` = ('4')"

And I get what I need, all beads with "Diameter 8mm" Then I want to filter my products more and click on attribute "Shape"(id = 3) with value "Round"(id = 11) And I get nothing, the query is look like:

"SELECT * FROM `products` 
JOIN `products_attributes_values`
ON (`products_attributes_values`.`product_id` = `products`.`id`)
WHERE `category_id` IN ('6', '7', '29', '8', '9', '36')
AND `products_attributes_values`.`attribute_id` = 2
AND `products_attributes_values`.`value_id` = ('4')
AND `products_attributes_values`.`attribute_id` = 3
AND `products_attributes_values`.`value_id` = ('11')"

so instead of nothing I want to get beads with diameter of 8mm and round shape. Separately all filters works fine. It just not working when I want to mix them like in example above. How I need to modify the query to get the job done?

Thanks.

Update:

I want to combine multiple values on one attributes with one or more of another.

For instance I want to get Beads with Diameter(id=2) of 8mm(id=4) or Diameter(id=2) 10mm(id=5) and Shape(id=3) of Round(id=11) or Shape(id=3) Faceted(id=62)

So at end I want to get both 8mm or 10mm beads with round or faceted shape.


回答1:


The 2 filters need to be handled separately, as each filter needs to look at completely different records in the products_attributes_values table.

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')

From UAMoto:

Its working but a bit slow, so I've used join aliases like one person told me:

"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')"

Updated queries for combinations of attributes:

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'))

or

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'))


来源:https://stackoverflow.com/questions/15994218/getting-products-with-desired-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!