Is it possible to negate a where clause?
e.g.
DELETE * FROM table WHERE id != 2;
The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 !=
The SQL not equal operator is <>. You should specify this in a WHERE statement. This lets you select rows where a particular column's contents is not equal to the value you have specified. You can also use !=
There is no != operator according to the ANSI/SQL 92 standard.
Here is the answer – Technically there is no difference between != and <>. Both of them work the same way and there is absolutely no difference in terms of performance or result.
You can do like this
DELETE FROM table WHERE id NOT IN ( 2 )
OR
DELETE FROM table WHERE id <> 2
As @Frank Schmitt noted, you might want to be careful about the NULL values too. If you want to delete everything which is not 2
(including the NULLs) then add OR id IS NULL
to the WHERE clause.
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