Skip to content Skip to sidebar Skip to footer

How To Find If A List/set Is Exactly Within Another List

My question is basically an extension of this question - How to find if a list/set is contained within another list I have the same data, but with more exact same products - order_

Solution 1:

You can still use having, but test for each product -- and for the absence of any other product:

SELECT o.order_id
FROM orders o
GROUPBY o.order_id
HAVINGSUM( product_id =222 ) >0ANDSUM( product_id =555 ) >0ANDSUM( product_id NOTIN (222, 555) ) =0 ;

Note that this uses the MySQL shorthand, where boolean expressions are treated as numbers with 1 for true and 0 for false. The standard syntax is:

HAVINGSUM( CASEWHEN product_id =222THEN1ELSE0END) >0ANDSUM( CASEWHEN product_id =555THEN1ELSE0END ) >0ANDSUM( CASEWHEN product_id NOTIN (222, 555) THEN1ELSE0END ) =0 ;

Post a Comment for "How To Find If A List/set Is Exactly Within Another List"