为此,您可以使用INSERT INTO SELECT语句。要格式化日期,请使用DATE_FORMAT()函数。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> DateOfJoining datetime, -> JoiningDate text -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(DateOfJoining) values('2019-10-26 13:52:10'); mysql> insert into DemoTable(DateOfJoining) values('2018-12-31 15:20:40');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+---------------------+-------------+ | DateOfJoining | JoiningDate | +---------------------+-------------+ | 2019-10-26 13:52:10 | NULL | | 2018-12-31 15:20:40 | NULL | +---------------------+-------------+ 2 rows in set (0.00 sec)
这是将日期记录插入到具有不同日期格式的同一表中的查询-
mysql> insert into DemoTable(JoiningDate) select date_format(DateOfJoining , '%W %e, %b %Y @ %r') from DemoTable; Records: 2 Duplicates: 0 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
+---------------------+-------------------------------------+ | DateOfJoining | JoiningDate | +---------------------+-------------------------------------+ | 2019-10-26 13:52:10 | NULL | | 2018-12-31 15:20:40 | NULL | | NULL | Saturday 26, Oct 2019 @ 01:52:10 PM | | NULL | Monday 31, Dec 2018 @ 03:20:40 PM | +---------------------+-------------------------------------+ 4 rows in set (0.00 sec)