i have gotten our system to return information correctly, howe开发者_如何学编程ver i now have the problem of returning information when we are applying filters. the query in question is:
SELECT `products`.*
CONCAT( '[', GROUP_CONCAT( DISTINCT '{', '"id":"', `product_variations`.`id`, '"', ',', '"id_product":"', `product_variations`.`id_product`, '"', ',', '"sku":"', `product_variations`.`sku`, '"', ',', '"options":"', `product_variations`.`options`, '"', ',', '"quantity":"', `product_variations`.`quantity`, '"', ',', '"price":"', `product_variations`.`price`, '"', '}' SEPARATOR ',' ), ']' ) as `_variations`,
CONCAT( '[', GROUP_CONCAT( DISTINCT '{', '"id":"', `product_images`.`id`, '"', ',', '"id_product":"', `product_images`.`id_product`, '"', ',', '"location":"', `product_images`.`location`, '"', ',', '"order":"', `product_images`.`order`, '"', ',', '"variation_key":"', `product_images`.`variation_key`, '"', ',', '"variation_values":"', `product_images`.`variation_values`, '"', '}' SEPARATOR ',' ), ']' ) as `_images`,
CONCAT( '[', GROUP_CONCAT( DISTINCT '{', '"id":"', `product_attributes`.`id`, '"', ',', '"id_product":"', `product_attributes`.`id_product`, '"', ',', '"key":"', `product_attributes`.`key`, '"', ',', '"value":"', `product_attributes`.`value`, '"', '}' SEPARATOR ',' ), ']' ) as `_attributes`
FROM (`products`)
LEFT JOIN product_variations ON products.id = product_variations.id_product
LEFT JOIN product_images ON products.id = product_images.id_product
LEFT JOIN product_attributes ON products.id = product_attributes.id_product
WHERE ( `products`.`id_category` = "11" OR `products`.`id_category` = "12" OR `products`.`id_category` = "9" )
AND ( product_attributes.key = "color" AND product_attributes.value IN ( "Red","Orange" ) )
AND ( product_attributes.key = "size" AND product_attributes.value IN ( "L","M" ) )
GROUP BY `products`.`id`
LIMIT 10
Ignoring the CONCAT things ( which return the attributes etc as json data ) what i am trying to get to work is, for example, if a product has the following attributes:
color=Red
color=Orange
color=Blue
size=L
size=M
size=S
so if i want to display products where
color=Red OR color=Orange
AND
size=L OR size=M
this product would be returned, the problem obviusly lies in the fact
AND ( product_attributes.key = "color"
AND ( product_attributes.key = "size"
cannot be true, but i want it to be ANY of the children, if it is only 1 filter, it works fine, but multiple "different" filters and it breaks,
also, it will only return the attributes in question which passed the filter.
i would like it to return ALL attributes for a product which has passed the filter, the more i look into this the more i think its not possible. Does anybody know of a way of doing this?
You would need to write the logic in a HAVING
clause, example:
HAVING COUNT(IF(product_attributes.key = "color" AND product_attributes.value IN ("Red","Orange"),
1, NULL)) > 0
AND COUNT(IF( product_attributes.key = "size" AND product_attributes.value IN ( "L","M" ),
1, NULL)) > 0
精彩评论