I have 2 queries:
SELECT sql_cache distinct p.products_image,
p.products_subimage1,
pd.products_name,
p.products_quantity,
p.products_model,
p.products_ordered,
p.products_id,
p.products_price,
p.products_date_added,
p.products_weight,
p.products_length,
p.products_width,
p.products_height,
p.products_tax_class_id,
p.products_status,
IF(s.status, s.specials_new_products_price, NULL) AS specials_new_products_price,
IF(s.status, s.specials_new_products_price, p.products_price) AS final_price
FROM products p
LEFT JOIN specials s
ON p.products_id = s.products_id
LEFT JOIN products_to_categories p2c
ON p.products_id=p2c.products_id
LEFT JOIN products_description pd
ON p.products_id=pd.products_id
INNER JOIN filter_association_products fap
ON p.products_id =fap.products_id
LEFT JOIN products_attributes pa
ON p.products_id = pa.products_id
WHERE p.products_status = '1'
AND date_sub(curdate(),INTERVAL 3000 day) <= p.products_date_added
AND fap.filter_id = 126
ORDER BY p.products_date_added DESC,
pd.products_name
Which gives me a result of 52 rows (products).
And an identical query with only difference:
AND fap.filter_id = 130
Which gives me a result of 4 rows.
One of the common things between these rows/products is that 3 out of the 4 with filter_id
130 also have filter_id
126 and I want to modify the query to give me results of only products with both (or even more, depends on the filter_id
s applied) of the indicated filter_id
.
I have tried
...
AND FIND_IN_SET(fap.filter_id,'126', '130')
ORDER BY p.products_date_added DESC, pd.products_name
But I get a result of 53 rows/products, meaning it's showing all the products that have either filter, while the result I am looking for in this case is of only the 3 rows that have both filter_id
.
What is the best way to rewrite the query to get the correct results?