SUN
SUN

Reputation: 973

MySQL Query not getting accurate output

I have three categories & subcategories in table. Category (adventure, rejuvenation, gourmet) AND sub_category(air, balloon etc.)

Now I am firing below query

select * from products where sub_category Like '%Air%' or 
sub_category Like '%Land%' and category = 'adventure' 
and type = 'experience' and status = 'active'

Now problem is it is also getting rows where category = Rejuvenation. Hence it should only get result where category = adventure

Upvotes: 0

Views: 32

Answers (2)

Gregor
Gregor

Reputation: 622

AND has priority over OR. That's why your query returns everything, that has a sub category like Air. You should update your query like this:

select * from products where (sub_category Like '%Air%' or 
sub_category Like '%Land%') and category = 'adventure' 
and type = 'experience' and status = 'active'

Upvotes: 0

Jens
Jens

Reputation: 69440

You have to use brackets arround the or Statements:

select * from products where (sub_category Like '%Air%' or 
sub_category Like '%Land%' )and category = 'adventure' 
and type = 'experience' and status = 'active'

Upvotes: 4

Related Questions