使用INTERVAL添加天数后,MySQL查询获取的日期记录大于当前日期?

让我们首先创建一个表-

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   AddDay int,
   PostDate date
);

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable(AddDay,PostDate) values(20,'2019-08-04');
mysql> insert into DemoTable(AddDay,PostDate) values(7,'2019-08-20');
mysql> insert into DemoTable(AddDay,PostDate) values(45,'2019-07-01');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

这将产生以下输出-

+----+--------+------------+
| 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)

这是从AddDay列添加天后获取大于当前日期的数据记录的查询-

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

这将产生以下输出-

+----+--------+------------+
| Id | AddDay | PostDate   |
+----+--------+------------+
|  1 |     20 | 2019-08-04 |
|  2 |      7 | 2019-08-20 |
+----+--------+------------+
2 rows in set (0.00 sec)
猜你喜欢