Select records from MySQL NOW() -1 Day?

MySQLMySQLi Database

To get records from NOW()-1 Day, you can use the following syntax −

select *from yourTableName where yourColumnName >=now()-interval 1 day;

To understand the above syntax, let us first create a table. The query to create a table.

mysql> create table GetRecordsFromNow
   −> (
   −> YourDateTime datetime
   −> );
Query OK, 0 rows affected (1.76 sec)

Now insert some dates into the fields. The query to insert records are as follows −

mysql> insert into GetRecordsFromNow values(date_add(now(),interval 3 day));
Query OK, 1 row affected (0.28 sec)

mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day));
Query OK, 1 row affected (0.11 sec)

mysql> insert into GetRecordsFromNow values(date_add(now(),interval -2 day));
Query OK, 1 row affected (0.14 sec)

mysql> insert into GetRecordsFromNow values(date_add(now(),interval -3 day));
Query OK, 1 row affected (0.09 sec)

mysql> insert into GetRecordsFromNow values(date_add(now(),interval -1 day));
Query OK, 1 row affected (0.08 sec)

Display all records with the help of select statement. The query is as follows −

mysql> select *from GetRecordsFromNow;

The following is the output −

+---------------------+
| YourDateTime        |
+---------------------+
| 2018-12-07 22:30:18 |
| 2018-12-03 22:30:31 |
| 2018-12-02 22:30:41 |
| 2018-12-01 22:30:56 |
| 2018-12-03 22:31:04 |
+---------------------+
5 rows in set (0.00 sec)

The following is the query to display records from now() -1 day −

mysql> select *from GetRecordsFromNow where YourDateTime >= now()-interval 1 day;

The following is the output −

+---------------------+
| YourDateTime        |
+---------------------+
| 2018-12-07 22:30:18 |
+---------------------+
1 row in set (0.04 sec)
raja
Published on 10-Jan-2019 17:04:17
Advertisements