DATE_FORMAT with MySQL

A friend gave me that hint with the DATE_FORMAT function in MySQL to get all post within a month; real handy:

SELECT * FROM posts WHERE DATE_FORMAT( postdate, \’%Y-%m\’ ) = \’2003-07\’;

Yet another query helps to get the posts per month based on a different solution (not using the date formatting):

SELECT substring( postdate, 1, 7 ) AS months, COUNT( ID ) FROM posts GROUP BY months

[1] http://www.mysql.com/doc/en/Date_and_time_functions.html

Leave a Reply