2

Is there anyway to have multiple conditions in a WHERE x LIKE %x% statement without using OR?

Basically I want to be able to select something where column1 LIKE %something% AND column2 LIKE %check1% OR %check2% OR %check3%

However, using OR removes my first previous check for column1 but I need this to stay in place

Here is what I'm currently using.. but I'm wondering if there is a better way of doing this so I don't have to keep repeating column1

SELECT id FROM test WHERE 
column1 LIKE '%bob%' AND column2 LIKE '%something%' 
OR column1 LIKE '%bob%' AND column2 LIKE '%somethingdifferent%' 
OR column1 LIKE '%bob%' AND column2 LIKE '%somethingdifferent2%'

Basically.. right now I keep having to repeat

column1 LIKE %bob%' AND .........

Just wondering if there is a better way to achieve this?

Ranga Sarin
  • 169
  • 1
  • 5
  • 14
  • What do you have against `OR` exactly? `select id from test where column1 like '%bob' and (column2 like '%something%' or column2 like '%somethingelse'% or column2 like '%morestuff%')` ? – Jon Clements Sep 02 '16 at 18:57

1 Answers1

6

What about:

SELECT id FROM test WHERE 
column1 LIKE '%bob%' AND
    (column2 LIKE '%something%' OR
     column2 LIKE '%somethingdifferent%' OR
     column2 LIKE '%somethingdifferent2%')

It's logically equivalent...

You can also use a RegEx: see How do I use regex in a SQLite query?

Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Yes, parentheses are the way to go! And it's a good idea to have them anyway when you start mixing `and` and `or`. Better to be explicit than relying on everybody remembering the priority between operators. – Xavier Sep 02 '16 at 19:02
  • using parentheses didn't even come to mind! thank you – Ranga Sarin Sep 02 '16 at 19:23