Search code examples
mysqlregexsql-likewhere-in

How to convert WHERE IN clause with multiple values to RegExp or Like


I want to convert this:

SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN (('come together', 'the beatles'),('all the small things', 'blink-182'))

To something like this but I don't know the right syntax:

SELECT id,songTitle,artistName, trackId
FROM songs
WHERE (songTitle, artistName) IN LIKE (('%come together%', '%the beatles%'),('%all the small things%', '%blink-182%'))

Except I'm searching 100s of more songs at once. We could use REGEXP too I just don't know the right syntax for either of those.


Solution

    • WHERE (a,b) IN ((1,2), ...) is very poorly optimized.
    • Leading wild cards in LIKE prevents use of an index.
    • You can't do the construct you attempted.

    So, performance aside, let's look at how to perform the task:

    WHERE ( songTitle LIKE '%come together%' AND artistName LIKE '%the beatles%')
       OR ( .... )
       OR ...
    

    Sorry, there is no short cut.

    REGEXP can't help in this case.

    FULLTEXT indexing is something to consider, but I don't see that it would help in this example.