我有两个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上,“关于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的新手并且坚持这个。
答案 0 :(得分: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`);
答案 1 :(得分:1)
你可以这样做;
delete from Journals where id not in (SELECT Journal from Documents)