如何在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)
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短