如何在不同的 MySQL 列中计算时间的差值?
你可以使用 TIME_FORMAT()。首先,我们创建一个表 -
mysql> create table DemoTable -> ( -> PunchIn time, -> PunchOut time, -> Launch time -> ); Query OK, 0 rows affected (0.50 sec)
使用 insert 命令,在表中插入一些记录 -
mysql> insert into DemoTable values('9:00','6:00','1:00:00'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('9:30','6:10','00:30:00'); Query OK, 1 row affected (0.21 sec)
使用 select 语句,从表中显示所有记录 -
mysql> select *from DemoTable;
输出
+----------+----------+----------+ | PunchIn | PunchOut | Launch | +----------+----------+----------+ | 09:00:00 | 06:00:00 | 01:00:00 | | 09:30:00 | 06:10:00 | 00:30:00 | +----------+----------+----------+ 2 rows in set (0.00 sec)
以下查询可以做出正确的计算 -
mysql> SELECT TIME_FORMAT(TIMEDIFF(TIMEDIFF(PunchIn,PunchOut), Launch), '%H:%i') as TimeDiff from DemoTable;
输出
+----------+ | TimeDiff | +----------+ | 02:00 | | 02:50 | +----------+ 2 rows in set (0.00 sec)
广告