Du kan stadig bruge having
, men test for hvert produkt -- og for fraværet af et andet produkt:
SELECT o.order_id
FROM orders o
GROUP BY o.order_id
HAVING SUM( product_id = 222 ) > 0 AND
SUM( product_id = 555 ) > 0 AND
SUM( product_id NOT IN (222, 555) ) = 0 ;
Bemærk, at dette bruger MySQL-stenografien, hvor booleske udtryk behandles som tal med 1
for sand og 0
for falsk. Standardsyntaksen er:
HAVING SUM( CASE WHEN product_id = 222 THEN 1 ELSE 0 END) > 0 AND
SUM( CASE WHEN product_id = 555 THEN 1 ELSE 0 END ) > 0 AND
SUM( CASE WHEN product_id NOT IN (222, 555) THEN 1 ELSE 0 END ) = 0 ;