如何删除MySQL中的所有孤立记录? [英] How to delete all orphan records in MySQL?
本文介绍了如何删除MySQL中的所有孤立记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3个表MySQL(MyIsam):
I have 3 tables MySQL (MyIsam):
user (id), message (id, userId, ...), archivedMessage (id, userId, ...)
如何删除所有没有消息且没有archivedMessage的用户?
How can I delete all the users having no message AND no archivedMessage?
推荐答案
您可以使用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)
这篇关于如何删除MySQL中的所有孤立记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文