mycount
不是表格的一列article
。正确的写法是:
SELECT article.topic,
article.ID ,
( SELECT COUNT(*)
FROM COMMENT
WHERE article.ID_art = comment.foreign_ID_art
GROUP BY foreign_ID_art
)
AS mycount
FROM article HAVING mycount = 0
实现相同结果的另一种方法,但以一种更有效的方式(假设存在适当的索引):
SELECT article.topic,
article.ID
FROM article
LEFT JOIN COMMENT
ON article.ID_art = comment.foreign_ID_art
WHERE comment.foreign_ID_art IS NULL
最终,最好将每篇文章的评论计数article
作为列存储在表中,comment_count
并在添加新评论时增加它。那么查询可以简化为:
SELECT article.topic,
article.ID
FROM article
WHERE article.comment_count = 0
使用索引comment_count
将非常有效。