Mysql Average on time column?

mysqllearner picture mysqllearner · Feb 7, 2010 · Viewed 30k times · Source

SELECT avg( duration ) as average FROM login;

The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc

I execute the query it gives me: 2725.78947368421

What is that? I want in time format, can mysql do the average on time??

Answer

Mark Byers picture Mark Byers · Feb 7, 2010

Try this:

SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(`login`))) FROM Table1;

Test data:

CREATE TABLE `login` (duration TIME NOT NULL);
INSERT INTO `login` (duration) VALUES
('00:00:20'),
('00:01:10'),
('00:20:15'),
('00:06:50');

Result:

00:07:09