如何从另一个表返回产品及其所有属性,同时按其中一个表进行过滤

发布于 2024-10-15 05:38:52 字数 2304 浏览 4 评论 0原文

我已经让我们的系统正确返回信息,但是我现在在应用过滤器时遇到返回信息的问题。有问题的查询是:

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 个过滤器,它工作正常,但是多个“不同”过滤器并且它会破坏,

而且,它只会返回通过过滤器的相关属性。

我希望它返回已通过过滤器的产品的所有属性,我对此了解得越多,我就越认为这是不可能的。有人知道这样做的方法吗?

i have gotten our system to return information correctly, however 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化 2024-10-22 05:38:52

您需要在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文