"Merge" IN and LIKE operator in Mysql

1.2k views Asked by At

I've a problem and I'm guessing if there's a better way to achieve my goal. I've this query in Mysql to retrieve some rows:

SELECT *
FROM table
WHERE field IN ('V','S','G','B')

What I would like to do is to run a query that retrieve the rows where the field has value LIKE those in the IN list. I know that the trivial way is to use this query:

SELECT *
FROM table
WHERE field LIKE '%V%' OR field LIKE '%S%' OR
      field LIKE '%G%' OR field LIKE '%B%'

What I want to know is there's an operator that do this or at least, if that operator does not exist, a better way to write the query.

Thanks to everyone that will help me.

1

There are 1 answers

1
onedaywhen On BEST ANSWER

Put the values in a table (Params) then use

SELECT *
  FROM table
 WHERE EXISTS (
               SELECT * 
                 FROM Params
                WHERE table.field LIKE '%' + Params.col + '%'
              );

Consider also putting the wildcard characters into the values in the Params table.