Forum Moderators: open

Message Too Old, No Replies

Group By Date

I would like to group my MySQL table by month, not by second

         

otem

1:48 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Each row in my database contains a datetime ("2005-05-15 12:13:14").

What I would like to do is to output some information in my database grouped by Month Year ("May 2005").

$result=mysql_query("SELECT *, COUNT(published) FROM blog GROUP BY published DESC");
while($blog=mysql_fetch_array($result)){
echo "<li><a href=\"/".substr($blog["published"],0,4)."/".substr($blog["published"],5,2)."/\">".date("F Y",strtotime($blog["published"]))."</a> (".$blog["COUNT(published)"].")</li>";
}

This seemed to work fine, but then I realized its doing exactly what I told it, and is grouping my data by published, right down to the second.

So if there are five posts in a month, all with different days and times, their data will be listed five times.

Does anybody have any recommendations for grouping my data by Month Year?

Thanks

justageek

2:33 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

SELECT *, COUNT(published) FROM blog GROUP BY month(published), year(published) DESC

JAG

otem

4:10 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Thank you, that worked perfectly!