Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top