多过滤器 mysql



我尝试创建一个基于属性的过滤器。设想这是一个多过滤器。

我的表如下所示:

product_id  attribute_id  attribute_option_id
----------  ------------  -------------------
4           16            51
4           13            28
5           16            51
6           16            51

如果您选择 51 attribute_option_id那么我将得到 4、5 和 6 product_id。当我选择attribute_option_id 51 和 28 时,我会得到product_id 4、5 和 6。这是一个不想要的结果。在这种情况下,我只想要product_id 4。现在我想我可以通过这条线解决这个问题

WHERE product_attribute.Attribute_option_id IN ('51 ', '28')

但预计这不是解决方案。

有人可以帮助我吗?

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

应该做这个伎俩。

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') 

最新更新