I am having trouble writing a query. Lets say I have a table full car makes and models, but I want to get rid of all of rows that are not associated with a list of models, I have written this...
DELETE FROM `cars` WHERE
`make` != 'Ford' OR
`make` != 'Toyota' OR
`make` != 'Cadillac'
However it is removing all of the rows. How do I write this so that I keep Ford, Toyota and Cadiillac, but delete Pontiac, Datsun and Renault?
Thank you for your help on this!
To delete rows in a MySQL table, use the DELETE FROM statement: DELETE FROM products WHERE product_id=1; The WHERE clause is optional, but you'll usually want it, unless you really want to delete every row from the table.
Another way to delete multiple rows is to use the IN operator. DELETE FROM table_name WHERE column_name IN (value 1, value 2, value 3, etc...); If you want to delete all records from the table then you can use this syntax.
Edit -> Preferences -> SQL Editor -> SQL Editor remove Forbid UPDATE and DELETE statements without a WHERE clause (safe updates) . BTW you can use TRUNCATE TABLE tablename; to delete all the records .
Easier to read:
DELETE FROM `cars` WHERE `make` NOT IN ('Ford', 'Toyota', 'Cadillac')
DELETE FROM cars
WHERE make <> 'Ford'
AND make <> 'Toyota'
AND make <> 'Cadillac'
Your query was removing all rows because:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With