Come calcolare la differenza tra il tempo in diverse colonne MySQL?
Puoi usare TIME_FORMAT(). Creiamo innanzitutto una tabella:
mysql> create table DemoTable
-> (
-> PunchIn time,
-> PunchOut time,
-> Launch time
-> );
Query OK, 0 rows affected (0.50 sec)
Inserisci alcuni record nella tabella utilizzando il comando di inserimento:
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)
Visualizza tutti i record dalla tabella utilizzando l'istruzione select :
mysql> select *from DemoTable;
Produzione
+----------+----------+----------+
| 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)
Ecco la query per effettuare un calcolo corretto del tempo:
mysql> SELECT TIME_FORMAT(TIMEDIFF(TIMEDIFF(PunchIn,PunchOut), Launch), '%H:%i') as TimeDiff from DemoTable;
Produzione
+----------+
| TimeDiff |
+----------+
| 02:00 |
| 02:50 |
+----------+
2 rows in set (0.00 sec)