MySql AND / OR selection whilst using IN

Lmillard
Lmillard used Ask the Experts™
on
Hi,

I am using IN with a query of mine to select products based on variant filters and I have noticed that it selects very much like the OR operator. is there a way to make this work the same as AND?

Query is very much like the attached code

Regards
Leigh
SELECT 
        DISTINCT(prodCode),
        RosettePadding,
        RosetteText,
        TitleDesc 
        FROM products 
        WHERE filterOptionID IN(1,20,25,13,19)
        ORDER BY TitleDesc, SortOrd, ProdCode

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
Commented:
in short: no, col IN (a,b) is equivalent to col = a or col = b.

now, how is the data stored in filterOptionID, is there 1 value, and hence several rows per product? or 1 row per product, and filterOptionID having several value (comma-separated, for example)

in both cases, the design is not normalized...
you should have a second table:

product_filter_options
prodcode, fileroptionid

and store all the values one by one there.

in that case, IN() could be used for the "or" version:
SELECT 
        prodCode,
        RosettePadding,
        RosetteText,
        TitleDesc 
        FROM products p
       WHERE exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID IN(1,20,25,13,19) )
        ORDER BY TitleDesc, SortOrd, ProdCode 

Open in new window


and your requiremend would translate into:

SELECT 
        prodCode,
        RosettePadding,
        RosetteText,
        TitleDesc 
        FROM products p
       WHERE exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID = 1 )
             AND exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID = 20 )
             AND exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID = 25 )
             AND exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID = 13 )
             AND exists ( select null FROM product_filter_options po where po.prodcode = p.prodcode and po.filterOptionID = 19 )
        ORDER BY TitleDesc, SortOrd, ProdCode 

Open in new window


hope this helps

Author

Commented:
Thanks for coming back so quickly with such a thorough answer.

The query I have shown is actually a Coldfusion cached result set hence appearing non normalized. The actual data is stored in filterGroup and filterGroupOptions to allow 1 row per product option and multiple product rows to allow multiple filters.

The full set of queries is also taking into account pagination and sortation variables being fired in so though I would make it as easy to understand for everyone and backwards engineer the solution.

Thanks for your help on this one, I can now see a bit of re-coding happening but can know see the path to follow.

Cheers
Leigh

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial