我有两个表一个论坛主题表和论坛评论。
我试图得到一个所有论坛主题的列表,并计算每个主题中有多少评论。
评论表包含每个主题的论坛主题的主键。
我试过了

SELECT
  `forum_topics`.`topic_id`,
  `forum_topics`.`topic_name`,
  `forum_topics`.`topic_info`,
  `forum_topics`.`topic_img`,
  `forum_topics`.`creation_date`,
  `forum_topics`.`is_deleted`
FROM
  `forum_topics`
JOIN
  `forum_comments`
ON
  `forum_comments`.`topic_id` = `forum_topics`.`topic_id`
GROUP BY
  `forum_topics`.`topic_id`
HAVING
  COUNT(`forum_comments`.`comment_id`) >= 0
  AND `forum_topics`.`review_status` = 'reviewed';

这似乎不返回任何结果,但也没有错误
希望有人能帮忙

最佳答案

试试这个

SELECT forum_topics.topic_id, forum_topics.topic_name, forum_topics.topic_info,
   forum_topics.topic_img, forum_topics.creation_date, forum_topics.is_deleted
FROM forum_topics
JOIN forum_comments ON forum_comments.topic_id = forum_topics.topic_id
WHERE forum_topics.review_status = 'reviewed'
GROUP BY forum_topics.topic_id
HAVING COUNT(forum_comments.comment_id) >= 0

10-06 03:49