I am trying to create graph for the number of posts by a user in last 6 months using Google Charts API.
My table structure is like this –
-> Userid - varchar(20)
-> Post_year - varchar(4)
-> Post_month - varchar(2)
To get the number of posts in last 6 months by a user, I have my query like this…
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`
As you can see, I am storing just the post year and month, and then using an offset variable to get the posts. This works like…
Case 1: if current year and month are 2010/12, then offset ones are 2010/7
Case 2: if current year and month are 2011/1, then offset ones are 2010/8
Now the query in Case 1 works fine as it is supposed to.
// Get all posts by a user
// ...
WHERE Post_year >= 2010
AND Post_month >= 7
But in case of Case 2, it doesn’t. Since the last part of query becomes…
// Get all posts by a user
// ...
WHERE Post_year >= 2010
AND Post_month >= 8
Hence, the posts of 2010/1 do not satisfy the condition Post_month >= 8.
How should I change my query for it to work in Case 2 as well.
Regards
You could do something like
But proper way is to introduce
Post_datefield and use it in query.