2014-02-26 47 views
0

我有2个表格:posts和forum_topics。每篇文章(回复)都与另一篇文章(一个论坛主题,然后与forum_topics表相关)相关联。计数论坛话题和回复

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

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' 

编辑

目前我只得到与职位表forum_topic(后)相关联的所有回复的计数。我想在forum_topics表中与论坛主题相关的帖子表中计算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; 
+0

作为此查询的结果,您看到了什么以及您希望看到什么? –

+0

看到我上面的修改。我希望能回答你的问题。 – Tifa

回答

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 
; 
+0

感谢您的解决方案,但我运行查询时得到的计数为0的'respond_count'和'forum_count'对数据库 – Tifa

+0

好吧,谢谢,现在我明白了更好的命令修改了我的答案查询,你能再看一次吗? –

+0

这正是我需要的,谢谢Daniel。 – Tifa

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 

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

,并从应用层面,你可以得到论坛发帖计数时,类型=邮电相同的答复


另一种方法

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' 

希望这将帮助你

问候

+0

对不起,'PostLeftOuterJoin.id'实际上是'PostLeftOuterJoin.object_id'(愚蠢,错字)的人。 'PostLeftOuterJoin.object_id'是在forum_topics表中将帖子链接到forum_topic的论坛主题ID。 – Tifa

+0

对该文档有帮助吗?如果是,请将答复标记为答案 – Monah

+0

将查询拆分为两个查询将迫使我更改模型,即创建另一种方法来更改数据的格式,以便视图不会中断。不幸的是,我现在没有时间这样做。 – Tifa