统计论坛主题和回复

时间:2014-02-26 08:36:03

标签: sql

我有2个表:帖子和forum_topics。每个帖子(回复)与另一个帖子(论坛主题,然后与forum_topics表相关联)相关联。

问题:我需要在posts表中统计所有论坛主题和回复。这就是我到目前为止所做的:

SELECT ForumTopic.id, ForumTopic.title, ForumTopic.modified, COUNT(ReplyLeftOuterJoin.id) as replies_count
FROM forum_topics AS ForumTopic
LEFT OUTER JOIN posts AS PostLeftOuterJoin
ON PostLeftOuterJoin.object_id = ForumTopic.id
    AND PostLeftOuterJoin.object_type = 'forum_topic'
    AND PostLeftOuterJoin.status = 'approved'
LEFT OUTER JOIN posts AS ReplyLeftOuterJoin
ON ReplyLeftOuterJoin.object_id = PostLeftOuterJoin.id
   AND ReplyLeftOuterJoin.object_type = 'post'
   AND ReplyLeftOuterJoin.status = 'approved'
WHERE ForumTopic.forum_category_id = 'some_id'

修改

目前我只收到与posts表中的forum_topic(帖子)相关的所有回复的计数。我想在forum_topics表中与论坛主题相关联的posts表中获取forum_topics的数量。

NB 仅供参考,此问题的解决方案应仅使用一个查询。

以下是两个表的架构:

DROP TABLE IF EXISTS `posts`;
CREATE TABLE `posts` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `context_id` bigint(20) unsigned DEFAULT NULL,
  `context_type` enum('resource','module','kwik','user','assignment') COLLATE utf8_unicode_ci DEFAULT NULL,
  `is_private` tinyint(1) NOT NULL,
  `is_unread` tinyint(4) NOT NULL,
  `last_replied` datetime NOT NULL,
  `object_id` bigint(20) unsigned DEFAULT NULL,
  `object_type` enum('forum_topic','forum','user','post') COLLATE utf8_unicode_ci DEFAULT NULL,
  `status` enum('approved','unapproved','disabled') COLLATE utf8_unicode_ci NOT NULL,
  `post` text COLLATE utf8_unicode_ci NOT NULL,
  `user_id` bigint(20) unsigned NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

DROP TABLE IF EXISTS `forum_topics`;
CREATE TABLE `forum_topics` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `view_count` int(10) unsigned NOT NULL,
  `forum_category_id` bigint(20) unsigned NOT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

2 个答案:

答案 0 :(得分:2)

SELECT
    ForumTopic.forum_category_id,
    COUNT(DISTINCT PostLeftOuterJoin.id) as forumtopics_count,
    COUNT(ReplyLeftOuterJoin.id) as replies_count
FROM forum_topics AS ForumTopic
LEFT OUTER JOIN posts AS PostLeftOuterJoin
ON PostLeftOuterJoin.object_id = ForumTopic.id
    AND PostLeftOuterJoin.object_type = 'forum_topic'
    AND PostLeftOuterJoin.status = 'approved'
LEFT OUTER JOIN posts AS ReplyLeftOuterJoin
ON ReplyLeftOuterJoin.object_id = PostLeftOuterJoin.id
   AND ReplyLeftOuterJoin.object_type = 'post'
   AND ReplyLeftOuterJoin.status = 'approved'
WHERE ForumTopic.forum_category_id = 'some_id'
GROUP BY
    ForumTopic.forum_category_id
;

答案 1 :(得分:0)

也许你可以在两个查询中得到结果

以下查询将为您提供每个论坛中的帖子数

select f.id,f.title,f.modified,Type='Posts',Number=count(*) 
from forum_topics as f
inner join posts p on p.forumid=f.id --u used in your query PostLeftOuterJoin.id ( is this the forum id or the posts id ?)
where p.status='approved' and p.object_type='forum_topic'
group by f.id,f.title,f.modified
union
select f.id,f.title,f.modified,Type='Replies',Number=count(*) 
from forum_topics as f
inner join posts p on p.forumid=f.id --u used in your query PostLeftOuterJoin.id ( is this the forum id or the posts id ?)
where p.status='approved' and p.object_type='posts'
group by f.id,f.title,f.modified

要区分帖子和回复,我添加了类型

从您的应用程序级别,您可以在Type = Posts时获得论坛帖子计数,并与回复相同


另一种方法

select f.id,f.title,f.modified,Posts=(select count(*) from posts p where f.id=p.object_id and p.status='approved' and p.object_type='forum_topic'),
Replies=(select count(*) from posts p on where f.id=p.object_id
and p.status='approved' and p.object_type='posts')
from forum_topics f
where f.forum_category_id='someid'

希望这会对你有所帮助

问候