我尝试创建一个基于属性的过滤器。 设想这是一个多过滤器。
我的表格如下:
product_id attribute_id attribute_option_id
---------- ------------ -------------------
4 16 51
4 13 28
5 16 51
6 16 51
如果您选择attribute_option_id 51,那么我将获得product_id 4,5和6。 当我选择attribute_option_id(s)51和28时,我将获得product_id(s)4,5和6。 这是一个不想要的结果。在这种情况下,我只想要product_id 4。 现在我想我可以通过这条线来解决这个问题
WHERE product_attribute.Attribute_option_id IN ('51 ', '28')
但预计这不是解决方案。
有人可以帮我这个吗?
答案 0 :(得分:4)
select a.product_id from (
select product_id, count(*) as cnt
from mytable
where attribute_option_id in (51, 28)
group by product_id having count(*) =2) a
应该这样做。
答案 1 :(得分:1)
SELECT *
FROM mytable
WHERE product_id IN (SELECT a.product_id
FROM mytable a
INNER JOIN mytable b
ON a.product_id = b.product_id
WHERE a.attribute_option_id = '51'
AND b.attribute_option_id = '28')