last month

All posts tagged last month

Here are a few MySQL date selection queries:

Today:
SELECT * FROM table WHERE adate=CURDATE();

 

Yesterday:
SELECT * FROM table WHERE adate > DATE_ADD(CURDATE(),INTERVAL -2 DAY) AND adate < CURDATE();

 

This week (Sunday-Today):
SELECT * FROM table WHERE YEARWEEK(adate) = YEARWEEK(CURRENT_DATE);

 

Last 7 Days:
SELECT * FROM table WHERE adate > DATE_ADD(CURDATE(),INTERVAL -7 DAY) AND adate < = CURDATE();

 

This Month:
SELECT * FROM table WHERE YEAR(adate) = YEAR(CURDATE()) AND MONTH(adate) = MONTH(CURDATE());

 

Last Month:
SELECT * FROM table WHERE adate BETWEEN date_format(NOW() – INTERVAL 1 MONTH, ‘%Y-%m-01’) AND last_day(NOW() – INTERVAL 1 MONTH);