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

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

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

mysql> create table DemoTable
(
   ProductStatus tinyint(1),
   ProductExpiryDate date
);
Query OK, 0 rows affected (1.03 sec)

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

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)

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程