I have the following five tables:
You could add to the WHERE clause:
AND NOT EXISTS (SELECT NULL FROM addon a2
WHERE a2.addon_id IN (1,14,237)
AND NOT EXISTS
( SELECT NULL
FROM product_addon pa2
WHERE pa2.addon_id = a2.addon_id
AND pa2.product_id = p.product_id
)
)
Or equivalently:
AND NOT EXISTS (SELECT NULL FROM addon a2
LEFT JOIN product_addon pa2
ON pa2.addon_id = a2.addon_id
AND pa2.product_id = p.product_id
WHERE a2.addon_id IN (1,14,237)
AND pa2.product_id IS NULL
)
)
GROUP BY set-of-column
HAVING SUM(CASE WHEN ISNULL(pa.addon_id, 0) IN (1,14,237) THEN 1 ELSE 0 END) = 3