使用MySQL获取不同列中的值之间的时间差
为此,您可以使用time_format()和time_diff()。要找到时差,您需要使用time_diff()方法。让我们首先创建一个表-
mysql> create table DemoTable624 (PunchIn datetime,PunchOut datetime);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable624 values('2019-07-14 12:10:00', '2019-07-14 12:50:00');
mysql> insert into DemoTable624 values('2019-07-14 11:00:00', '2019-07-14 11:30:00');使用select语句显示表中的所有记录-
mysql> select *from DemoTable624;
这将产生以下输出-
+---------------------+---------------------+ | PunchIn | PunchOut | +---------------------+---------------------+ | 2019-07-14 12:10:00 | 2019-07-14 12:50:00 | | 2019-07-14 11:00:00 | 2019-07-14 11:30:00 | +---------------------+---------------------+ 2 rows in set (0.00 sec)
以下是获取时差的查询-
mysql> select time_format(timediff(PunchIn,PunchOut),'%H hrs, %i min.') from DemoTable624;
这将产生以下输出-
+-----------------------------------------------------------+ | time_format(timediff(PunchIn,PunchOut),'%H hrs, %i min.') | +-----------------------------------------------------------+ | -00 hrs, 40 min. | | -00 hrs, 30 min. | +-----------------------------------------------------------+ 2 rows in set (0.00 sec)
热门推荐
2 红丝绒戚风蛋糕
3 夹心小甜点
4 南瓜甜点
6 粘米粉蒸蛋糕
7 酸奶华夫饼
9 红柚鸡蛋糕
10 葡萄干蛋糕
11 紫薯蛋糕卷
13 纸杯裱花小蛋糕
14 果酱奶酪蛋糕
15 日式樱花蔓越莓大福
16 草莓蛋糕卷
17 卡仕达长崎杯子蛋糕