我正在尝试使用 Google Charts API 为用户在过去 6 个月内发布的帖子数量创建图表。 我的表结构是这样的-
-> Userid - varchar(20)
-> Post_year - varchar(4)
-> Post_month - varchar(2)
要获取用户过去 6 个月的帖子数,我有这样的查询...
SELECT COUNT(*) AS count, `Post_year`, `Post_month`
FROM (`table`)
WHERE `Userid` = '1234'
AND `Post_year` >= '$year_offset'
AND `Post_month` >= '$month_offset'
GROUP BY `Post_year`, `Post_month`
如您所见,我只存储帖子的年份和月份,然后使用偏移量变量来获取帖子。这就像...
情况1:如果当前年份和月份是2010/12,那么抵消的是2010/7 情况2:如果当前年月为2011/1,则抵消为2010/8
现在情况 1 中的查询可以正常工作。
// Get all posts by a user
// ...
WHERE Post_year >= 2010
AND Post_month >= 7
但在情况 2 的情况下,情况并非如此。由于查询的最后一部分变为...
// Get all posts by a user
// ...
WHERE Post_year >= 2010
AND Post_month >= 8
因此,2010/1 的帖子不满足条件 Post_month >= 8
。
我应该如何更改我的查询以使其在案例 2 中也能正常工作。
问候
最佳答案
你可以做类似的事情
AND `Post_year` * 100 + `Post_month` >= $year_offset * 100 + $month_offset
但正确的做法是引入Post_date
字段,并在查询中使用。
关于Mysql查询以获取按月分组的最近6个月的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4710905/