MySQL query to fetch date records greater than the current date after adding days with INTERVAL?



Let us first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   AddDay int,
   PostDate date
);
Query OK, 0 rows affected (2.73 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(AddDay,PostDate) values(20,'2019-08-04');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(AddDay,PostDate) values(7,'2019-08-20');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable(AddDay,PostDate) values(45,'2019-07-01');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+--------+------------+
| Id | AddDay | PostDate   |
+----+--------+------------+
|  1 |     20 | 2019-08-04 |
|  2 |      7 | 2019-08-20 |
|  3 |     45 | 2019-07-01 |
+----+--------+------------+
3 rows in set (0.00 sec)

Here is the query to fetch data records greater than current date after adding days from AddDay column −

mysql> select *from DemoTable where PostDate +interval AddDay day >=curdate();

This will produce the following output −

+----+--------+------------+
| Id | AddDay | PostDate   |
+----+--------+------------+
|  1 |     20 | 2019-08-04 |
|  2 |      7 | 2019-08-20 |
+----+--------+------------+
2 rows in set (0.00 sec)
raja
Published on 30-Sep-2019 11:43:16
Advertisements