2014-02-26 74 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