MySQL: Select today, yesterday or day before yesterday records

MySQL: Select today, yesterday or day before yesterday records

Usually when you work with records where a date or time-stamp field is present you need to select records from:

  • today records
  • yesterday records
  • day before yesterday records

Lets say you have a table (my_table) like this:

ID Name Date
1 Jon Dow 2011-01-10
2 Jane Dow 2011-01-10

where `Date` is date type fieldWatch movie online John Wick: Chapter 2 (2017)

Today records:

SELECT *
FROM `my_table`
WHERE `Date` = CURDATE()

Yesterday records:

SELECT *
FROM `my_table`
WHERE `Date` > DATE_ADD(CURDATE(), INTERVAL -2 DAY) AND
	  `Date` < CURDATE()

Day before yesterday records:

SELECT *
FROM `my_table`
WHERE `Date` > DATE_ADD(CURDATE(), INTERVAL -3 DAY) AND
	  `Date` < DATE_ADD(CURDATE(), INTERVAL -2 DAY)

If your `Date` field is a time-stamp or datetime then replace `Date` with date(`Date`)

No comments.