1

我有 2 个表一个论坛主题表和论坛评论。

我正在尝试获取所有论坛主题的列表,并计算每个主题中有多少评论。

评论表包含每个主题的论坛主题的PK。

我试过了

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';

这似乎没有返回任何结果,但也没有错误

希望有人可以帮助

4

2 回答 2

3

试试这个

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
于 2013-02-21T13:16:37.893 回答
3

尝试以下操作:

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`,
  `forum_topics`.`review_status`,
  COUNT(`forum_comments`.`comment_id`) count_comments
FROM
  `forum_topics`
LEFT JOIN
  `forum_comments`
ON
  `forum_comments`.`topic_id` = `forum_topics`.`topic_id`
order by case when `forum_topics`.`review_status` = 'reviewed' then 1 else 2 end, 
         count_comments desc
于 2013-02-21T13:20:48.553 回答