Reputation: 35275
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: 2761
Reputation: 21214
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