How to delete all orphan records in MySQL?

后端 未结 1 1621

I have 3 tables MySQL (MyIsam):

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

How can I delete all the users having no mes

1条回答
  •  青春惊慌失措
    2021-02-08 10:14

    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)
    

    0 讨论(0)
提交回复
热议问题