我有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';
这似乎没有返回任何结果,但也没有错误
希望有人可以提供帮助
答案 0 :(得分: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
答案 1 :(得分: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