Search code examples
mysqlsql-likein-operator

combine Not In and like function in mysql query


I want to know is there there a way to merge not in() function with like or wildcard * in MySQL? Like in the following query:

select sl_tags from tablname
    where `sl_tags` not in ('%Soccer%','%Football%','%Hockey%','%shinny%','%Basketball%','%Volleyball%','%Cricket%')

the above query does not work.

I know this works

select sl_tags from tablname where `sl_tags` Not like '%Soccer%' and `sl_tags` Not like '%Football%' 

I saw previous questions and answers on stack which asked, and did not find any relevant answers.


Solution

  • No, there's no such operator.

    But you can do this in slightly different manner using Regular Expressions

    SELECT sl_tags
      FROM tablname
      WHERE sl_tags NOT REGEXP 'Soccer|Football|Hockey|shinny|Basketball';
    

    Be aware that for some characters (like dot .) have special meaning and need to be escaped to be used literally.