Let’s say the current date is −
'2019-10-20
We will first see an example and create a table −
mysql> create table DemoTable1582 -> ( -> PostedDate datetime -> ); Query OK, 0 rows affected (13.36 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1582 values('2019-01-21 12:34:40'); Query OK, 1 row affected (1.06 sec) mysql> insert into DemoTable1582 values('2019-10-15 11:00:00'); Query OK, 1 row affected (0.87 sec) mysql> insert into DemoTable1582 values('2019-10-25 1:10:00'); Query OK, 1 row affected (1.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1582;
This will produce the following output −
+---------------------+ | PostedDate | +---------------------+ | 2019-01-21 12:34:40 | | 2019-10-15 11:00:00 | | 2019-10-25 01:10:00 | +---------------------+ 3 rows in set (0.00 sec)
Here is the query to fetch rows posted before last 3 days.
mysql> select * from DemoTable1582 -> where PostedDate between date_sub(PostedDate, interval 3 day) and now();
This will produce the following output −
+---------------------+ | PostedDate | +---------------------+ | 2019-01-21 12:34:40 | | 2019-10-15 11:00:00 | +---------------------+ 2 rows in set (0.20 sec)