使用mysql查询两个表以按小时计算总主题

时间:2012-12-10 18:15:18

标签: mysql

我的论坛有两个表来存储主题(帖子和forums_archive_posts)

$this->DB->build( array(
                                    'select'    => "HOUR( FROM_UNIXTIME( post_date ) ) as hour, COUNT(*) AS postCount",
                                    'from'      => 'posts',
                                    'where'     => "new_topic=0 AND author_id=" . $member['member_id'],
                                    'group'     => 'HOUR( FROM_UNIXTIME( post_date ) )',
                        )       );

此查询仅适用于第一个表(帖子),我需要一个也可以在“forum_archive_posts”中使用的查询

表格结构(forum_archive_posts =帖子):

archive_author_id = author_id

archive_content_date = post_date

1 个答案:

答案 0 :(得分:0)

不知道你是如何用这种格式做的,而是像

那样
Select postHour, sum(postcount) as postCount From
(
  select Hour ...  as PostHour
  From 
  Posts...
  Union
  Select Hour ...
  From
  ForumArchivePosts ...
) dummyTableName
Group by PostHour

应该做的工作。它们会从两个表中按小时计算,然后将它们加在一起。