Mysql 平均时间列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2217139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mysql Average on time column?
提问by mysqllearner
SELECT avg( duration ) as average FROM login
;
SELECT avg( duration ) 作为平均值 FROM login
;
The datatype for duration is "time", thus my value is like: 00:00:14, 00:20:23 etc
持续时间的数据类型是“时间”,因此我的值是:00:00:14、00:20:23 等
I execute the query it gives me: 2725.78947368421
我执行它给我的查询:2725.78947368421
What is that? I want in time format, can mysql do the average on time??
那是什么?我要时间格式,mysql可以按时间做平均吗??
回答by Mark Byers
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