如何删除MySQL中的所有孤立记录?

时间:2011-01-26 13:02:30

标签: sql mysql

我有3个表MySQL(MyIsam):

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

如何删除所有没有消息且没有archivedMessage的用户?

1 个答案:

答案 0 :(得分:15)

您可以使用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)