MySQL - SELECT ... YESTERDAY

September 2017

MySQL provides easy access to database records and makes it possible to carry out manipulations on the data. To select time-related data from the database, MySQL provides a simple keyword known as INTERVAL. The INTERVAL MySQL command allows the data to be compared within the time specified in the parameter and selects only those records that match the selection criteria. To select database records which are one day old for example, the parameter in the INTERVAL MySQL command should be 1. For previous records, the number of days can be specified according to the parameter.
  • Use the following syntax:
    • SELECT * FROM myTable WHERE field_date = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 1 DAY)
  • In order to select previous records:
    • SELECT * FROM myTable WHERE field_date = DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 2 DAY)

Related

Published by netty5. Latest update on March 5, 2012 at 10:20 AM by Virginia Parsons.
This document, titled "MySQL - SELECT ... YESTERDAY," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).