How to sum rows of VARCHAR datatype or TIME datatype in MySQL?

MySQLMySQLi Database

Let us first create a −

mysql> create table DemoTable1442
   -> (
   -> DueTime time
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1442 values('00:08:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1442 values('00:04:00');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1442 values('12:55:00');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select −

mysql> select * from DemoTable1442;

This will produce the following output −

+----------+
| DueTime  |
+----------+
| 00:08:00 |
| 00:04:00 |
| 12:55:00 |
+----------+
3 rows in set (0.00 sec)

Following is the query to sum rows of VARCHAR datatype or TIME datatype in MySQL −

mysql> select sec_to_time(sum(time_to_sec(DueTime) ) ) as TotalTime from DemoTable1442;

This will produce the following output −

+-----------+
| TotalTime |
+-----------+
| 13:07:00  |
+-----------+
1 row in set (0.03 sec)
raja
Published on 12-Nov-2019 11:28:03
Advertisements