如何在SQL中使用日期记录更新列值并为当前日期之前的对应记录设置1

假设当前日期为2019-08-20。现在对于我们的示例,我们将创建一个表-

create table DemoTable
(
   ProductStatus tinyint(1),
   ProductExpiryDate date
);

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

insert into DemoTable values(0,'2019-06-12');
insert into DemoTable values(0,'2019-10-11');
insert into DemoTable values(0,'2018-07-24');
insert into DemoTable values(0,'2018-09-05');

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

select *from DemoTable;

这将产生以下输出-

+---------------+-------------------+
| ProductStatus | ProductExpiryDate |
+---------------+-------------------+
| 0             | 2019-06-12        |
| 0             | 2019-10-11        |
| 0             | 2018-07-24        |
| 0             | 2018-09-05        |
+---------------+-------------------+
4 rows in set (0.00 sec)

以下是为当前日期之前的记录设置值1的查询

update DemoTable
   set ProductStatus=1
   where ProductExpiryDate <=CURDATE();
Rows matched : 3 Changed : 3 Warnings : 0

让我们再次检查表记录-

select *from DemoTable;

这将产生以下输出-

+---------------+-------------------+
| ProductStatus | ProductExpiryDate |
+---------------+-------------------+
| 1             | 2019-06-12        |
| 0             | 2019-10-11        |
| 1             | 2018-07-24        |
| 1             | 2018-09-05        |
+---------------+-------------------+
4 rows in set (0.00 sec)