1

我有一个 mysql 表 'totolog' :

id  dt                    data
1   2013-05-01 06:01:01   hi john  
2   2013-05-01 06:04:23   hi bob  
3   2013-05-01 07:17:36   hi alex  
4   2013-05-01 14:49:41   hi all  

如何检索行之间的平均时间差?

我怀疑是这样的:

SELECT UNKNOW_FUNCTION(dt) FROM totolog ORDER BY dt ASC;
4

2 回答 2

1

给定这样的数据集...

CREATE TABLE totolog
 (id  INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,dt                    DATETIME NOT NULL
 ,data VARCHAR(20) NOT NULL
 );

 INSERT INTO totolog VALUES
 (1   ,'2012-12-01 06:01:01','hi john'),
 (2   ,'2013-01-01 06:04:23','hi bob'),
 (3   ,'2013-02-01 07:17:36','hi alex'),
 (4   ,'2013-03-28 14:49:41','hi all');

我想你会想要这样的东西......

SELECT FROM_UNIXTIME(AVG(UNIX_TIMESTAMP(dt)))x FROM totolog;
+---------------------+
| x                   |
+---------------------+
| 2013-01-22 20:33:10 |
+---------------------+
1 row in set (0.00 sec)

...或这个...

SELECT FROM_UNIXTIME((MAX(UNIX_TIMESTAMP(dt))+MIN(UNIX_TIMESTAMP(dt)))/2)x 
  FROM totolog;
+---------------------+
| x                   |
+---------------------+
| 2013-01-28 22:25:21 |
+---------------------+
1 row in set (0.00 sec)
于 2013-05-10T11:37:44.697 回答
1

平均时间是time_max - time_min / number_of_records

SELECT (max(dt) - min(dt)) / count(dt) as avg_dt
FROM totolog 
ORDER BY avg_dt ASC;
于 2013-05-10T09:57:57.403 回答