Mysql Use two AND's to retrieve only records matching both

Tony Pearce
Tony Pearce used Ask the Experts™
I have a query that looks for matching criteria in two columns, when using this query either/or gets selected, I need it to only pick the records that match both??

SELECT ProdCAT1 FROM $tableName WHERE ProdCAT1 LIKE '%$siteword%' AND ProdSELECT LIKE '%$brandword%'

Any pointers? I can't use PHP, it must be done in the query...
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2012
what you have is correct

are you sure of your results?

can you post sample data that produces a result where only one of the two LIKE conditions is satisfied?
Greg AlexanderLead Developer
Or would get both

SELECT ProdCAT1 FROM $tableName WHERE ProdCAT1 LIKE '%$siteword%' OR ProdSELECT LIKE '%$brandword%'


Sorry, I typed question too quickly, what I mean is:

both conditions MUST be correct for the record to be returned, if either one is incorrect then it is not.

Or have I just got it totally wrong,

The query as is will return results even if second one does not....
Billing Engineer
Most Valuable Expert 2014
Top Expert 2009
as above experts posted: you condition seems fine.
what I would say then is that $brandword is empty: please double check that the variable is set before using it in the line that set's the sql ...


Thats it, thank you all very much, changed to an exact = and no wildcards and all is fine in the world..

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