Ranga Sarin Ranga Sarin - 3 months ago 7
Python Question

SQLite multiple conditions in WHERE LIKE

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?

Answer

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?