如何在 SQL 中用日期记录更新列值并将当前日期之前的相应记录设置为 1
mysqlmysqli database
假设当前日期是 2019-08-20。现在,为了举例说明,我们将创建一个表 −
mysql> create table DemoTable ( ProductStatus tinyint(1), ProductExpiryDate date ); Query OK, 0 rows affected (1.03 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(0,'2019-06-12'); Query OK, 1 row affected (0.43 sec) mysql> insert into DemoTable values(0,'2019-10-11'); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable values(0,'2018-07-24'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(0,'2018-09-05'); Query OK, 1 row affected (0.27 sec)
使用 select 语句显示表中的所有记录 −
mysql> 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
mysql> update DemoTable set ProductStatus=1 where ProductExpiryDate <=CURDATE(); Query OK, 3 rows affected (0.95 sec) Rows matched : 3 Changed : 3 Warnings : 0
让我们再次检查表记录 −
mysql> 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)