您可以使用CONCAT()
函数设置日期和时间字段以获取DATETIME字段。
让我们创建一个演示表
mysql> create table getDateTimeFieldsDemo -> ( -> ShippingDate date, -> ShippingTime time, -> Shippingdatetime datetime -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into getDateTimeFieldsDemo(ShippingDate,ShippingTime) values('2018-01-21','09:45:34'); mysql> insert into getDateTimeFieldsDemo(ShippingDate,ShippingTime) values('2013-07-26','13:21:20'); mysql> insert into getDateTimeFieldsDemo(ShippingDate,ShippingTime) values('2017-12-31','15:31:40'); mysql> insert into getDateTimeFieldsDemo(ShippingDate,ShippingTime) values('2019-03-07','12:13:34');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from getDateTimeFieldsDemo;
以下是输出
+--------------+--------------+------------------+ | ShippingDate | ShippingTime | Shippingdatetime | +--------------+--------------+------------------+ | 2018-01-21 | 09:45:34 | NULL | | 2013-07-26 | 13:21:20 | NULL | | 2017-12-31 | 15:31:40 | NULL | | 2019-03-07 | 12:13:34 | NULL | +--------------+--------------+------------------+ 4 rows in set (0.00 sec)
这是添加DATE和TIME字段以在MySQL中获取DATETIME字段的查询
mysql> update getDateTimeFieldsDemo set Shippingdatetime=concat(ShippingDate," ",ShippingTime); Rows matched: 4 Changed: 4 Warnings: 0
现在再次检查表记录。查询如下-
mysql> select *from getDateTimeFieldsDemo;
以下是输出
+--------------+--------------+---------------------+ | ShippingDate | ShippingTime | Shippingdatetime | +--------------+--------------+---------------------+ | 2018-01-21 | 09:45:34 | 2018-01-21 09:45:34 | | 2013-07-26 | 13:21:20 | 2013-07-26 13:21:20 | | 2017-12-31 | 15:31:40 | 2017-12-31 15:31:40 | | 2019-03-07 | 12:13:34 | 2019-03-07 12:13:34 | +--------------+--------------+---------------------+ 4 rows in set (0.00 sec)