【问题标题】:SQL Adding count (from 2 tables) to existing select (of 3 tables)SQL将计数(来自2个表)添加到现有选择(3个表)
【发布时间】:2014-10-28 11:05:06
【问题描述】:

查询论坛数据库。我正在使用此查询来获取线程名称、海报、日期等。

(暂时只留下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

我该怎么做?

【问题讨论】:

  • SELECT thread_subject,COUNT(*) FROM...

标签: mysql sql select join count


【解决方案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

【讨论】:

  • 谢谢! :)(符号不够)
猜你喜欢
  • 1970-01-01
  • 2019-01-04
  • 1970-01-01
  • 2022-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-08
相关资源
最近更新 更多