SQL将计数(从2个表)添加到现有选择(3个表)

时间:2014-10-28 11:05:07

标签: mysql sql select join count

对论坛数据库进行查询。我正在使用此查询来获取主题名称,海报,日期等。

(现在只留下thread_subject)

SELECT `thread_subject` FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users` ON 
fusion_posts.post_author=fusion_users.user_id 
GROUP BY fusion_posts.thread_id ORDER BY `post_id` DESC LIMIT 16

基本上,我还需要在现有选择中添加类似下面的计数,以计算每个帖子的帖子。

SELECT COUNT(*) AS PostCount FROM fusion_posts,fusion_threads WHERE fusion_threads.thread_id = fusion_posts.thread_id group by fusion_threads.thread_id

我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

试试这个: -

SELECT `thread_subject`, COUNT(*) AS PostCount 
FROM `fusion_posts` JOIN `fusion_threads`
ON fusion_posts.thread_id=fusion_threads.thread_id JOIN `fusion_users` 
ON fusion_posts.post_author=fusion_users.user_id 
GROUP BY fusion_posts.thread_id, `thread_subject` 
ORDER BY `post_id` DESC 
LIMIT 16