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??
create table demo (startDate datetime); insert demo (startDate) values ('2015-04-10 3:46:07'); insert demo (startDate) values ('2015-04-09 3:47:37'); insert demo (startDate) values ('2015-04-08 3:48:07'); insert demo (startDate) values ('2015-04-07 3:43:44'); insert demo (startDate) values ('2015-04-06 3:39:08'); ...
MySQL AVG() Function The AVG() function returns the average value of an expression.
Introduction to MySQL TIME data typeMySQL uses the 'HH:MM:SS' format for querying and displaying a time value that represents a time of day, which is within 24 hours. To represent a time interval between two events, MySQL uses the 'HHH:MM:SS' format, which is larger than 24 hours.
MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With