0

我有两个 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”发表在 Journal of Foo 上,“New Discoveries about 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 的新手,因此陷入了困境。

4

2 回答 2

1

您可以按以下方式进行;

delete from Journals where id not in (SELECT Journal from Documents) 
于 2013-05-29T05:29:03.120 回答
1
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`);
于 2013-05-29T05:26:43.050 回答