9 Useful MySQL Date Related Query

This article is about 9 useful dates related MySQL query.

When we need to get record from database of a particular time period, these MySQL useful query will make task easy to do.

In this tutorial we will learn MySQL query to get current record, last week, last month, last year record.

Get Last one week,one month ,one year records from Database Date Field

Get Last one week records

SELECT * FROM table WHERE Date BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()

Get Last one month records

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

Get Last 30 days records

SELECT * FROM table WHERE Date BETWEEN (NOW() - INTERVAL 1 MONTH) AND NOW()

Get Last one year records

SELECT * FROM table WHERE Date BETWEEN (NOW() - INTERVAL 1 YEAR) AND NOW()

Select records from today, this week, this month, this year from Database Date Field

Select records from today

$processdate = DATE('Y-m-d');
SELECT SUM(amount) AS totalfundreceive FROM `table` WHERE DATE(`processdate`)='$processdate'

Select records from this week

SELECT * FROM table WHERE processdate > DATE_SUB(NOW(), INTERVAL 1 WEEK);

Select records from this month/ Current month

SELECT * FROM table WHERE YEAR(date) = YEAR(CURRENT_DATE()) AND MONTH(date) = MONTH(CURRENT_DATE());

Select records from this year/ current year

SELECT * FROM TABLE WHERE YEAR(date) = YEAR(CURDATE()); 

Records of two date ranges

SELECT * FROM TABLE WHERE date between '2019-01-07' AND '2019-01-15'


Leave a Comment