Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - How to delete rows where column does not contain certain values?

Tags:

mysql

I'm trying to delete all rows where a specific column does not contain certain strings. Like this:

DELETE * FROM table
WHERE Disease NOT LIKE (Malaria, HIV, E. coli O157);

I get this error: #1064 - You have an error in your SQL syntax; near '* FROM table WHERE Disease NOT LIKE (Malaria' at line 1

like image 325
Adam Avatar asked Oct 25 '25 04:10

Adam


1 Answers

If the description given is the exact description of the disease, just use NOT IN (with proper quotes):

DELETE FROM table
WHERE Disease NOT IN ('Malaria', 'HIV', 'E. coli O157');

but if it is a substring, you have to use multiple LIKE:

DELETE FROM table
WHERE
  NOT (Disease LIKE '%Malaria%'
       or Disease LIKE '%HIV%'
       or Disease LIKE '%E. coli O157%');
like image 185
fthiella Avatar answered Oct 26 '25 19:10

fthiella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!