Getting previous day/month/year records from mysql table

In this post will explain how to get Today, yesterday, last one month record, last year record from database table using Mysql query.

Here I have used the MySQL function CURDATE() to get the today’s date.

And also to get the difference in today date and previous day or month I have used the MySQL function DATE_SUB

Query:

select * from tablename where `datefield` >= DATE_SUB(CURDATE(), INTERVAL 1 DAY);

The above query will return records of today date only.

Query:

select * from tablename where `datefield` BETWEEN DATE_SUB(CURDATE(), INTERVAL 2 day) AND CURDATE( );

The above query will return records of today and yesterday records.

Query:

select * from tablename where `datefield` BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 month) AND CURDATE( ) or (`datefield` >= DATE_SUB(CURDATE(), INTERVAL 0 DAY));

The above query will return last one month records from today date.

Same as this you can write query for last one year records also. just replace ‘1 month’ with ‘1 year’ in above query.

Posted in MySQL