Get all MySQL records from the previous day (yesterday)?

MySQLMySQLi Database

To get the records from the previous day, the following is the syntax

select *from yourTableName where date(yourColumnName)= DATE(NOW() - INTERVAL 1 DAY);

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table yesterDayRecordsDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> ArrivalDateTime datetime
   -> );
Query OK, 0 rows affected (0.44 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2014-11-09 13:45:21');
Query OK, 1 row affected (0.11 sec)
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2017-10-19 11:41:31');
Query OK, 1 row affected (0.15 sec)
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-25 10:40:45');
Query OK, 1 row affected (0.14 sec)
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-26 12:06:07');
Query OK, 1 row affected (0.15 sec)
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-25 12:06:47');
Query OK, 1 row affected (0.19 sec)
mysql> insert into yesterDayRecordsDemo(ArrivalDateTime) values('2019-02-27 11:45:49');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement

mysql> select *from yesterDayRecordsDemo;

The following is the output

+----+---------------------+
| Id | ArrivalDateTime     |
+----+---------------------+
| 1  | 2014-11-09 13:45:21 |
| 2  | 2017-10-19 11:41:31 |
| 3  | 2019-02-25 10:40:45 |
| 4  | 2019-02-26 12:06:07 |
| 5  | 2019-02-25 12:06:47 |
| 6  | 2019-02-27 11:45:49 |
+----+---------------------+
6 rows in set (0.00 sec)

Here is the query to get MySQL records from yesterday

mysql> select *from yesterDayRecordsDemo where date(ArrivalDateTime)= DATE(NOW() - INTERVAL 1 DAY);

The following is the output

+----+---------------------+
| Id | ArrivalDateTime     |
+----+---------------------+
| 4  | 2019-02-26 12:06:07 |
+----+---------------------+
1 row in set (0.08 sec)
raja
Published on 29-Mar-2019 11:41:32
Advertisements