lewisqic lewisqic - 4 months ago 7
SQL Question

MySQL: Query to get all rows from previous month

I need to select all rows in my database that were created last month.

For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a

date_created
column in my database that lists the date created in this format:
2007-06-05 14:50:17
.

Answer
SELECT * FROM table
WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH)
AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)