Search code examples
mysqldelete-row

Deleting rows where a column does not contain any of 2 strings in Mysql


What's the command to achieve this: MYSQL: delete all rows where "row_name" column does not contain string "foo" or "bar"


Solution

  • Use the LIKE operator here:

    DELETE
    FROM yourTable
    WHERE row_name NOT LIKE '%foo%' AND row_name NOT LIKE '%bar%';
    

    Using REGEXP we can write this a bit more concisely:

    DELETE
    FROM yourTable
    WHERE row_name NOT REGEXP 'foo|bar';