MySQL 查询仅更新日期中的月份?
要仅更新日期中的月份,请使用 MONTH()。我们首先创建一个表 -
mysql> create table DemoTable861(AdmissionDate date); Query OK, 0 rows affected (1.22 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable861 values('2019-01-21'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable861 values('2018-01-01'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable861 values('2016-01-02'); Query OK, 1 row affected (1.27 sec) mysql> insert into DemoTable861 values('2018-01-14'); Query OK, 1 row affected (0.47 sec)
使用选择语句从表中显示所有记录 -
mysql> select *from DemoTable861;
这将产生以下输出 -
+---------------+ | AdmissionDate | +---------------+ | 2019-01-21 | | 2018-01-01 | | 2016-01-02 | | 2018-01-14 | +---------------+ 4 rows in set (0.00 sec)
以下是仅更新月份的查询 -
mysql> update DemoTable861 set AdmissionDate=date_add(AdmissionDate,Interval -1 month) where month(AdmissionDate)=01; Query OK, 4 rows affected (0.42 sec) Rows matched : 4 Changed : 4 Warnings : 0
让我们再次检查一下表记录 -
mysql> select *from DemoTable861;
这将产生以下输出 -
+---------------+ | AdmissionDate | +---------------+ | 2018-12-21 | | 2017-12-01 | | 2015-12-02 | | 2017-12-14 | +---------------+ 4 rows in set (0.00 sec)
广告