为 MySQL 表格列中的所有日期记录添加一年
让我们先创建一个表 −
mysql> create table DemoTable -> ( -> JoiningDate datetime -> ); Query OK, 0 rows affected (0.59 sec)
使用插入指令在表中插入一些记录 −
mysql> insert into DemoTable values('2015-01-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('2017-04-02'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.16 sec)
使用选择语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+---------------------+ | JoiningDate | +---------------------+ | 2015-01-21 00:00:00 | | 2017-04-02 00:00:00 | | 2018-12-31 00:00:00 | +---------------------+ 3 rows in set (0.00 sec)
以下即为实现 DATE_ADD() 和添加一年的查询 −
mysql> select date_add(JoiningDate, interval 1 year) from DemoTable;
这将产生以下输出 −
+----------------------------------------+ | date_add(JoiningDate, interval 1 year) | +----------------------------------------+ | 2016-01-21 00:00:00 | | 2018-04-02 00:00:00 | | 2019-12-31 00:00:00 | +----------------------------------------+ 3 rows in set (0.00 sec)
广告