Search code examples
mysqldatetimearchive

mysql getting all possible months from datetime field


so i have a table with hundreds records. And a have a filed name "created" type with a datetime format. Now I want to make and archive with the months. For example January, February.... etc. I need to create query to find all possible months. For example if my records start from 2011/05/01 to now I will need to fetch the months that means months 5,6,7,8,9,10,11,12. Is there a way to that ???


Solution

  • Yes, use the DATE_FORMAT function and other date and time functions.

    More details here

    For example, if you want all your records for December 2011:

    SELECT * FROM posts WHERE YEAR(created) = 2011 AND MONTH(created) = 12