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

12

我有3张MySQL表(MyISAM):

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

如何删除所有没有message和archivedMessage的用户?

1个回答

18

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

使用“LEFT JOIN…IS NULL”方法有什么优缺点? - Toto
1
@Toto:从功能上来说,它们是相同的,但可能存在性能差异。 - Andomar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接