I have a table that stores the product identifier with the filter identifier. Matching is one for many. (One product may be associated with many filters). Now I need to get records matching all criteria . Say, for example, I need to get products that have FILTERS 1,5 and 7. I want the exact match to match products that match 1 AND 5 AND 7.
The table structure is shown below.
CREATE TABLE IF NOT EXISTS `product_to_filter` (
`product_id` int(11) NOT NULL,
`filter_id` int(11) NOT NULL,
PRIMARY KEY (`product_id`,`filter_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I tried the query mentioned below, but did not get the desired result.
select distinct p1.product_id, p1.filter_id from product_to_filter p1
join product_to_filter p2 on 1=1
join product_to_filter p3 on 1=1
where
p1.filter_id=1 AND p2.filter_id=5 AND p3.filter_id=7
Please help me in solving this problem.
source
share