Search code examples
sqlbindingfindconditional-statementssql-like

more than one binding for one attribute in like sql conditions, trying to DRY it up


Not This:

"height like '%1%' or height like '%2%' or height like '%5%'"

Say there are a lot of bindings for just this one attribute.

They are not in order, they are random.

How can this sql be simplified?

Something like this:

"height like ('%2%' or '%1%' or '%5%')

Code is just an example.

I'm not looking for injection suggestions.


Solution

  • There is no equivalent to IN for LIKE. Your first method is the correct approach.

    WHERE (height LIKE '%foo%' OR height LIKE '%bar%' OR height LIKE '%baz%')
    

    Note that this will be slow. You may want to look at a full text search instead.