我有两个mySQL表,分别代表在特定期刊上发布的文档:

Documents:

ID Title                          Journal
1  Monty: A New Study             6
2  New Discoveries about Python   17
3  An Unpublished Document        NULL

Journals:

ID Title
6  Journal of Foo
10 Orphans Weekly
17 Journal of Bar
99 Journal of Orphans


在这里,文档“ Monty:A New Study”发表在Foo杂志上,而“ Python的新发现”发表在著名杂志Journal of Bar上。

问题是,无论出于何种原因,都会有没有相关文档的期刊标题,即#10和#99。我要删除没有关联文档的所有日记帐分录。我想做类似的事情:

delete from Journals where id is not one of (select journal from documents where journal is not null)


但是我是mySQL的新手,因此陷入了困境。

最佳答案

DELETE FROM `Journals` AS j WHERE j.ID NOT EXISTS(SELECT Journal FROM `Documents`);


要么...

DELETE FROM `Journals` AS j WHERE j.ID NOT IN (SELECT Journal FROM `Documents`);

关于mysql - 如何找到数据库中相关表之间的孤立条目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16806068/

10-10 03:23