我在DB中有以下(此问题已简化)表:
article
(id,title,text,author_id等)publication
(id,article_id,publication_place,date)我想查找特定作者在过去X个月中发布的文章数量,但前提是它们之前未在任何地方发布过。
目前我有:
SELECT COUNT(DISTINCT article.id) AS articlecount
FROM article
LEFT JOIN publication ON publication.article_id=article.id
WHERE author_id = :authorID
AND publication.date >= :date
LIMIT 1
...基本上选择在:date
之后发布的文章,但它不排除之前发布的文章。是否可以在不添加子选择(AND article.id NOT IN ...
)的情况下实现更好的查询执行时间?
答案 0 :(得分:1)
您可以使用左连接AND article.id NOT IN ...
子选项执行相同的操作:
SELECT COUNT(DISTINCT article.id) AS articlecount
FROM article a
JOIN publication p_new ON p_new.article_id=a.id
LEFT JOIN publication p_old ON p_old.article_id=a.id AND p_old.date < :date
WHERE a.author_id = :authorID
AND p_new.date >= :date
AND p_old.id IS NULL