Reputation: 35235
2 tables:
items(id, ...)
users(id, item_id, ...)
How do you delete all records in items that are not referenced from users?
Upvotes: 9
Views: 2759
Reputation: 21184
Beware that NOT IN may be really slow. Sometimes - surpringly enough - its faster to do something like this:
DELETE FROM items WHERE id IN
(SELECT id FROM items EXCEPT SELECT item_id FROM users)
Upvotes: 11