我有一个节点表:
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
有什么想法吗?
谢谢。
最佳答案
这将为您提供示例输出,但根据您对问题的描述,我不确定这是否正是您正在寻找的内容。
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
关于mysql - 从多个表中获取带时间戳的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11725262/