Brob
Brob

Reputation: 675

MySQL not returning as expected

I am running the following query

SELECT * FROM (`default_schemes`) 
WHERE `region` = 'Location1' 
OR `region` = 'Location2' 
OR `region` = 'Location3' 
OR `region` = 'Location4' 
OR `region` = 'Location5' 
OR `region` = 'Location6' 
AND `finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring %' 

Which will match the finance and guidance but pulls in all locations regarless of what is set, what am I missing?

Upvotes: 0

Views: 48

Answers (4)

Niju
Niju

Reputation: 487

SELECT * FROM (`default_schemes`) 
WHERE `region` IN ('Location1' 
, 'Location2' 
, 'Location3' 
, 'Location4' 
, 'Location5' 
, 'Location6' )
AND (`finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring %')

Can you please check with this.

Upvotes: 0

Joni
Joni

Reputation: 111339

The and operator has higher precedence than the or operator, so something like A or B and C or D is parsed as A or (B and C) or D.

Add parentheses to the appropriate places to get the interpretation you want.

Upvotes: 0

Strawberry
Strawberry

Reputation: 33945

Perhaps you want this...

SELECT * 
  FROM default_schemes
 WHERE region IN('Location1','Location2','Location3','Location4','Location5','Location6')
   AND (finance LIKE '%Equity%' OR guidance LIKE '%Business Mentoring %');

Alternatively, you might want this...

SELECT * 
  FROM default_schemes
 WHERE (region IN('Location1','Location2','Location3','Location4','Location5','Location6')
   AND finance LIKE '%Equity%')
    OR guidance LIKE '%Business Mentoring %';

Upvotes: 0

chetan
chetan

Reputation: 2896

SELECT * FROM `default_schemes`
WHERE `region` in ('Location1' ,'Location2' ,'Location3' , 'Location4' , 'Location5' ,'Location6' )
AND (`finance` LIKE '%Equity%' 
OR `guidance` LIKE '%Business Mentoring%');

Upvotes: 2

Related Questions