我已经让我们的系统正确地返回信息,但是我现在遇到了在应用过滤器时返回信息的问题。有问题的查询是:
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
忽略CONCAT事物(将属性等返回为json数据)我想要开始工作的是,例如,如果产品具有以下属性:
color=Red
color=Orange
color=Blue
size=L
size=M
size=S
所以如果我想展示产品
color=Red OR color=Orange
AND
size=L OR size=M
此产品将退回, 问题显然在于事实
AND ( product_attributes.key = "color"
AND ( product_attributes.key = "size"
不可能是真的,但我希望它是任何一个孩子,如果它只有1个过滤器,它工作正常,但多个“不同”的过滤器,它打破,
另外,它只返回通过过滤器的有问题的属性。
我希望它返回已通过过滤器的产品的所有属性,我越是调查它越多我认为它不可能。有人知道这样做的方法吗?
答案 0 :(得分:1)
您需要在HAVING
子句中编写逻辑,例如:
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