简体   繁体   English

计算两个日期列之间多行的平均差

[英]Calculate the average difference between two date columns for multiple rows

I want to calculate the average difference between multiple dates: sent_date & view_date. 我想计算多个日期之间的平均差:send_date和view_date。

My table structure look like: 我的表结构如下:

CREATE TABLE `mails` (
  `m_id` int(8) NOT NULL AUTO_INCREMENT,
  `sent_date` date NOT NULL DEFAULT '0000-00-00',
  `view_date` date NOT NULL DEFAULT '0000-00-00',
  PRIMARY KEY (`l_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

Example data: 示例数据:

sent_date: 2013-06-01 view_date: 2013-06-02 difference: 2 days 发送日期:2013-06-01查看日期:2013-06-02区别:2天

sent_date: 2013-06-01 view_date: 2013-06-05 difference: 4 days 发送日期:2013-06-01查看日期:2013-06-05区别:4天

Average: 3 days 平均:3天

Use DATEDIFF() and AVG() 使用DATEDIFF()AVG()

select avg(datediff(view_date, sent_date))
from mails

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM