我对opencart中的“过滤器”模块做了一些扩展。
但是仍然有一个小问题。

我当前的SQL:

SELECT product.product_id FROM `oc_product` product
LEFT JOIN `oc_product_attribute` attribute ON product.product_id = attribute.product_id
WHERE
((attribute.attribute_id = 13 (weight) AND `text` BETWEEN 1 AND 2)
OR (attribute.attribute_id = 12 (length) AND `text` BETWEEN 10 AND 12));


但这会返回介于1和2(权重)之间以及介于10和12(长度)之间的事物。例如,重量为2kg的东西被退回,长度为11cm的东西被退回。

我希望它返回的东西既可以重2公斤,也可以长11厘米。

我试过将AND放在条件之间,但不会返回任何内容。

谢谢

最佳答案

使用aggregation和having子句来获得满足两个条件的product_id:

select p.product_id
from oc_product p
left join oc_product_attribute a on p.product_id = a.product_id
group by p.product_id
having sum(a.attribute_id = 13 and text between 1 and 2) > 0
   and sum(a.attribute_id = 12 and text between 10 and 12) > 0;

08-03 19:43
查看更多