0

我有一个节点表:

nid type  created   status
2   blog  134292319 1
3   forum 134292536 1
4   blog  135921392 0

为了绘制随时间变化的已发布 (status=1) 节点的数量,我执行以下查询:

SELECT created, type
FROM node WHERE status = 1
ORDER BY created

然后,我在 PHP 中浏览这个数据集,将其分成带有时间戳的组,每个组都有一个节点计数。结果被缓存,因此执行缓慢不是问题。

我还有一张评论表:

nid timestamp status
2   134292363 1
3   134293234 1

我想将论坛评论计数合并到节点计数图中。

要获得评论计数,我会运行以下查询:

SELECT timestamp
FROM comments
INNER JOIN node ON comments.nid = node.nid
WHERE
  node.type = 'forum'
  AND comments.status = 1
ORDER BY timestamp

我需要以某种方式将这两个查询结合起来,最终得到(对于给出的示例):

created   type
134292319 blog
134292536 forum
134293234 forum_comment

有任何想法吗?

谢谢。

4

1 回答 1

1

这将为您提供示例输出,但根据您对问题的描述,我不确定它是否正是您正在寻找的内容。

SELECT created, type FROM
(
   SELECT created, type
   FROM node WHERE status = 1

   UNION ALL

   SELECT timestamp as created, 'forum_comment' as type
   FROM comments
   INNER JOIN node ON comments.nid = node.nid
   WHERE node.type = 'forum'
     AND comments.status = 1
) AS U
ORDER BY U.created
于 2012-07-30T16:59:47.093 回答