I need some help writing a MySQL query to show me rows from last month, but not the whole month, only up and until the same day, hour and minute as it is now(), but 1 month
Simple code please check
SELECT * FROM table_name WHERE created <= (NOW() - INTERVAL 1 MONTH)
SELECT *
FROM table
WHERE date BETWEEN
ADDDATE(LAST_DAY(DATE_SUB(NOW(),INTERVAL 2 MONTH)), INTERVAL 1 DAY)
AND DATE_SUB(NOW(),INTERVAL 1 MONTH);
See the docs for info on DATE_SUB
, ADDDATE
, LAST_DAY
and other useful datetime functions.
This is an example of a MySQL date operation relevant to your question:
SELECT DATE_ADD( now( ) , INTERVAL -1 MONTH )
The above will return date time one month ago
So, you can use it, as follows:
SELECT *
FROM your_table
WHERE Your_Date_Column BETWEEN '2011-01-04'
AND DATE_ADD(NOW( ), INTERVAL -1 MONTH )
maybe like this - all last month DATE(recDate) BETWEEN DATE_ADD(LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 2 MONTH)), INTERVAL 1 DAY) AND LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH) )