2015-11-08 113 views
0

我正面临一个难题;不知道为什么 - 是因为这么晚了,还是我只是卡住了。我的目标是在网页上创建一个过滤器,所以我试图弄清楚这一点。选择在同一列中具有相同值的项目

我在过滤器存储在参考表product_filter中的产品列表。

结构:

id | product_id | filter1_id | filter2_id 
1 | 1  | 2   |  1 <--- 
2 | 1  | 4   |  3 
3 | 1  | 5   |  1 
4 | 2  | 2   |  1 <--- 
5 | 2  | 3   |  1 
6 | 3  | 2   |  1 <--- 
7 | 3  | 3   |  4 

我需要提交的产品(例如1,2,3)列表,只有得到这些过滤器的组合,是对所有选择的产品的ID相同。所以结果需要被

filter1_id | filter2_id 
    2  |  1  

我的问题是,我的产品可能会有所不同,我不能做一个自我吨内部联接的...所以我坚持...任何建议?

+0

尝试在where条款中使用count(filter1_id)'> 1 ... –

+0

Andrew,解决方案上的任何喜悦? – wwkudu

回答

0

这是一种方法,你可以尝试:

select filter1_id, filter2_id 
from product_filter 
group by filter1_id, filter2_id 
    having count(*)=(
    select count(distinct product_id) 
    from product_filter 
) 

时filter1_id和filter2_id的组合存在的product_id这只会返回一个列表。 (Fiddle here.)那是你在做什么?如果没有针对所有给定的product_id存在任何组合,您是否没有提及应该返回什么 - 一个空的结果集?

这不是一个自我加入(或者甚至是它们的一大堆;)),但它仍然会相当昂贵,我想象。

相关问题