jsmitter3
jsmitter3

Reputation: 443

SQL delete if id does not match array

I have a SQL table that looks like this

ID | name | age
---------------
1  | john   | 22
2  | adam   | 33
3  | joe    | 43
4  | paul   | 23
1  | peter  | 44
2  | simon  | 67
1  | rod    | 32
7  | aaron  | 15
3  | tom    | 55

I am trying to delete everything that does not have an id of either 1,3,4

So I should be left with

ID | name | age
---------------
1  | john   | 22
3  | joe    | 43
4  | paul   | 23
1  | peter  | 44
1  | rod    | 32
3  | tom    | 55

I have this so far but it is not working

DELETE FROM `mytable` WHERE `id` != 1,3,4;

Where am I going wrong?

Upvotes: 0

Views: 1044

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270573

You want not in:

DELETE FROM `mytable`
    WHERE `id` NOT IN ( 1, 3, 4 );

!= -- or more commonly <> is used only for single values.

Upvotes: 3

Related Questions