将datetime插入MySQL中的另一个datetime字段?
您可以借助update命令来实现。要了解该方法,让我们创建一个表。创建表的查询如下-
mysql> create table AddDateTimeWithOther −> ( −> Id int, −> IssueDate datetime, −> DueDate datetime −> );
使用insert语句在表中插入一些记录。查询如下-
mysql> insert into AddDateTimeWithOther values(100,now(),date_add(now(),interval -3 year)); mysql> insert into AddDateTimeWithOther values(101,now(),date_add(now(),interval 2 year));
在select语句的帮助下显示表中的所有记录。查询如下-
mysql> select *from AddDateTimeWithOther;
以下是输出-
+------+---------------------+---------------------+ | Id | IssueDate | DueDate | +------+---------------------+---------------------+ | 100 | 2018-12-11 17:33:28 | 2015-12-11 17:33:28 | | 101 | 2018-12-11 17:33:37 | 2020-12-11 17:33:37 | +------+---------------------+---------------------+ 2 rows in set (0.00 sec)
现在,如果您要设置其他日期来代替“DueDate”'2015-12-1117:33:28,请使用UPDATE命令。以下查询通过将截止日期更新为'2019-12-1117:35:03'显示相同的内容。
查询如下-
mysql> update AddDateTimeWithOther set DueDate = date_add(now(),interval 1 year) where Id = 100; Rows matched: 1 Changed: 1 Warnings: 0
现在,您可以检查数据是否已插入。查询如下-
mysql> select *from AddDateTimeWithOther;
以下是显示更新成功工作和插入日期的输出-
+------+---------------------+---------------------+ | Id | IssueDate | DueDate | +------+---------------------+---------------------+ | 100 | 2018-12-11 17:33:28 | 2019-12-11 17:35:03 | | 101 | 2018-12-11 17:33:37 | 2020-12-11 17:33:37 | +------+---------------------+---------------------+ 2 rows in set (0.00 sec)