Mysql Multiple OR in query

Tony Pearce
Tony Pearce used Ask the Experts™
on
Hi,
I have a query that needs to check if a column has one of three values, also the query includes other stuff:

SELECT *
FROM
insearchall
WHERE
category LIKE '%$category%' AND Sub_Category LIKE '%$subcategory%' OR Sub_Category LIKE '%$subcategory1%' OR Sub_Category LIKE '%$subcategory2%'
AND Category_Split LIKE '$shapes%' AND manufacturer LIKE'$towelmanu' AND
length1 LIKE '$minwidth' AND doortype LIKE '$doortype'

This does not work, it pulls all the info ignoring the other requirements, using only the OR's, really stuck on how to format this, I guess it's done using a subquery but cannot seem to crack the format.

Any takers?

Thanks
Tony
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
This is fine, you just need to group your ANDs and ORs using parentheses:


SELECT *
FROM
insearchall
WHERE
category LIKE '%$category%' AND

(Sub_Category LIKE '%$subcategory%' OR Sub_Category LIKE '%$subcategory1%' OR Sub_Category LIKE '%$subcategory2%')

AND Category_Split LIKE '$shapes%' AND manufacturer LIKE'$towelmanu' AND
length1 LIKE '$minwidth' AND doortype LIKE '$doortype'

Don't know exactly what your and/or logic is, but something like this should work.
Commented:
Also, I think you would be better to use = instead of LIKE, where the value is fixed.

SELECT *
FROM
insearchall
WHERE
category LIKE '%$category%' AND
(Sub_Category LIKE '%$subcategory%' OR Sub_Category LIKE '%$subcategory1%' OR Sub_Category LIKE '%$subcategory2%')

AND Category_Split LIKE '$shapes%' AND manufacturer = '$towelmanu' AND
length1 = '$minwidth' AND doortype = '$doortype'

Anywhere you don't need the % wildcard character, use =.

Author

Commented:
Perfect...

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