Hamster
Hamster

Reputation: 3122

How do I select from a table and delete all other non-selected entries?

Is there an elegant way to delete all non-selected entries from a table after selecting from it?

Upvotes: 0

Views: 120

Answers (2)

Andrew Lazarus
Andrew Lazarus

Reputation: 19292

I think you mean DELETE and not DROP. DROP can only erase an entire object (e.g., TABLE). If you aren't worried about performance, you can have something like

DELETE FROM mytable WHERE mytable_key NOT IN 
  (SELECT mytable_key FROM mytable WHERE some_or_another_condition);

Many DBs allow a JOIN-type syntax that will probably perform better if you have to do this on a frequent basis.

Upvotes: 1

DRapp
DRapp

Reputation: 48129

Scary and generic statement... but this will do from whatever table... I wouldn't think you would actually do to a production table though...

delete from SomeTable where NOT (some condition)

Upvotes: 0

Related Questions