开发者

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

开发者 https://www.devze.com 2023-03-07 17:25 出处:网络
Is there an elegant w开发者_开发技巧ay to delete all non-selected entries from a table after selecting from it?I think you mean DELETE and not DROP. DROP can only erase an entire object (e.g., TABLE).

Is there an elegant w开发者_开发技巧ay to delete all non-selected entries from a table after selecting from it?


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.


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号