12

I have 3 tables MySQL (MyIsam):

user (id), message (id, userId, ...), archivedMessage (id, userId, ...)

How can I delete all the users having no message AND no archivedMessage?

1 Answer 1

18

You could use not exists:

delete from user
where not exists (select * from message m where m.userid = user.id)
      and not exists (select * from archivedMessage am where am.userid = user.id)
2
  • Are there pros/cons against the LEFT join... IS NULL method?
    – Toto
    Commented Jan 26, 2011 at 13:16
  • 1
    @Toto: Functionally they're the same, but there might be performance differences
    – Andomar
    Commented Jan 26, 2011 at 13:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.