MySQL - SELECT ... YESTERDAY

December 2016

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 :

This document entitled « MySQL - SELECT ... YESTERDAY » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.