如何在MySQL中向同一表格中插入不同日期格式的日期记录?
为此,可以使用 INSERT INTO SELECT 语句。要设置日期格式,请使用 DATE_FORMAT() 函数。我们首先创建一个表 -
mysql> create table DemoTable -> ( -> DateOfJoining datetime, -> JoiningDate text -> ); Query OK, 0 rows affected (0.79 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable(DateOfJoining) values('2019-10-26 13:52:10'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(DateOfJoining) values('2018-12-31 15:20:40'); Query OK, 1 row affected (0.16 sec)
使用 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; Query OK, 2 rows affected (0.13 sec) 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)
广告