添加DATE和TIME字段以在MySQL中获取DATETIME字段?
您可以使用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)
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短