为此,您可以使用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)