如何将 'And' 子句放入多对多

How to put 'And' clause to many-to-many

你能再帮我一下吗?

得到 2 tables!这里:

Products
ID             |          Name
1                         X Product
2                         Y Product
3                         Z Product

Filters
ID             |          Name
1                         X Filter
2                         Y Filter
3                         Z Filter

Product_Filter
Product_ID     |          Filter_ID
1                         1
1                         2
2                         1
2                         2
2                         3
3                         3

这个枢轴 table 包含:

X 产品有 X 过滤器和 Y 过滤器

Y 产品有 X 滤镜和 Y 滤镜和 Z 滤镜

Z 产品有 Z 过滤器

我有这个 sql 代码,我可以通过定义 X 过滤器的 ID 获得 X 和 Y 产品。

SELECT DISTINCT `products`.* 
FROM `products` 
  JOIN `filters` ON `products`.`id` = `filters`.`id` 
WHERE `product_filter`.`filter_id` = 1;

如果我想获得仅包含 X Filter 和 Z Filter 的产品怎么办? 喜欢像这样放置和子句,但它当然不起作用。:

WHERE `product_filter`.`filter_id` = 1 
  and 'product_filter'.'filter_id' = 69;

您需要使用 group by 和 having count 同时在单个列上同时匹配 2 个条件

select
p.* from Products p
join Product_Filter pf on pf.Product_ID = p.ID
join Filters f on f.ID = pf.Filter_ID
where pf.filter_id in (1,69)
group by p.ID
having count(*) = 2 

http://sqlfiddle.com/#!9/22c96/1