Forum Moderators: open
I have a mysql table that has different stories, and a published date in the form of "YYYY-MM-DD HH:MM:SS"
What I would like to do would be for any given month, to build an array with the date, and the number of stories published on that date.
This is what I have so far, but I know it isn't correct.
$query=mysql_query("SELECT COUNT(*) FROM table WHERE pubdate>'2009-01-01 00:00:00' AND pubdate<'2009-01-31 24:00:00' GROUP BY pubdate");
$array=...
Any help would be greatly appreciated.
Probably, you should use something like that (untested):
$query=mysql_query("SELECT COUNT(*) FROM table WHERE pubdate>'2009-01-01 00:00:00' AND pubdate<'2009-01-31 24:00:00' GROUP BY day(pubdate)");
Or even:
$query=mysql_query("SELECT COUNT(*) FROM table WHERE year(pubdate) = 2009 and month(pubdate) = 1 GROUP BY day(pubdate)");
The following code:
SELECT pubdate, COUNT(pubdate) AS count FROM table WHERE pubdate BETWEEN '2009-01-01' AND '2009-01-31' GROUP BY pubdate ;
Gives me this sample output:
2009-01-01 01:00:00, 1
2009-01-01 10:00:03, 2
2009-01-01 10:28:46, 1
2009-01-01 10:30:03, 2
2009-01-01 10:58:40, 1
2009-01-01 11:00:03, 1
...
Will I need to break up this column into one that's just date, and one that's just time to do this? I thought mysql was very flexible when using the right column types.