Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to get all rows from previous month

Tags:

mysql

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.

like image 443
lewisqic Avatar asked Jan 19 '10 00:01

lewisqic


People also ask

How do I get last 3 months data in SQL?

In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records.

How do I get last 30 days in SQL Server?

SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).


1 Answers

SELECT * FROM table WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) 
like image 180
hobodave Avatar answered Sep 17 '22 10:09

hobodave