0

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

1
  • Note also that '%...' cannot use an index, so if there's any way you can avoid that, so much the better. Commented Aug 29, 2016 at 13:33

2 Answers 2

4

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'
Sign up to request clarification or add additional context in comments.

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.