Get distinct (unique) month and year of records in a mysql table

Here is a quick snippet to pulls the month and year from any date field in a MySQL table…

SELECT DATE_FORMAT(date_added, '%b %Y') AS sDate, COUNT(post_id) AS iCount FROM blog_posts GROUP BY sDate ORDER BY sDate DESC
sDate iCount
Jan 2012 4
Dec 2011 16
Nov 2011 0
Oct 2011 12

Mostly for my reference, but hope it helps someone else 😉

One thought on “Get distinct (unique) month and year of records in a mysql table”

Leave a Reply

Your email address will not be published. Required fields are marked *