如何计算不同 MySQL 列中时间的差异?
mysqlmysqli database
您可以使用 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)