How to calculate time based on seconds in MySQL?


Let us first create a table −

mysql> create table DemoTable
(
   Logouttime time
);
Query OK, 0 rows affected (0.62 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('5:50:00');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('6:10:10');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('8:00:50');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| Logouttime |
+------------+
| 05:50:00   |
| 06:10:10   |
| 08:00:50   |
+------------+
3 rows in set (0.00 sec)

Following is the query to calculate time-based on seconds in MySQL −

mysql> select sec_to_time(sum(time_to_sec(Logouttime))) from DemoTable;

This will produce the following output −

+-------------------------------------------+
| sec_to_time(sum(time_to_sec(Logouttime))) |
+-------------------------------------------+
| 20:01:00                                  |
+-------------------------------------------+
1 row in set (0.80 sec)

Updated on: 25-Sep-2019

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements