MySql 如何返回 时间差的平均值

本文介绍如何在MySQL中计算时间差的平均值。针对包含时间字段的表,当时间为空时忽略,提供的SQL语句能够返回所有有效数据的时间差平均值。同时,提供了查看原始数据的方法。
摘要由CSDN通过智能技术生成

MySql 如何返回 时间差的平均值

返回时间差平均值

设有一表内有字段submitTimeeditTime,需要返回所有数据的时间差的平均值
时间为空则不计,语句为

//返回平均值为小时
SELECT AVG(TIMESTAMPDIFF(HOUR,submitTime,editTime)) as timediff from orders where editTime is not null;

//返回平均值为分钟
SELECT AVG(TIMESTAMPDIFF(MINUTE,submitTime,editTime)) as timediff from orders where editTime is not null;

//返回平均值为秒
SELECT AVG(TIMESTAMPDIFF(SECOND,submitTime,editTime)) as timediff from orders where editTime is not null;

返回值是

MariaDB [mypchelper]>  SELECT AVG(TIMESTAMPDIFF(HOUR,submitTime,editTime)) as timediff from orders where editTime is not null;
+----------+
| timediff |
+----------+
|  62.9911 |
+----------+
1 row in set (0.00 sec)

查看原数据

若要查看这些数据,则输入

 SELECT id,TIMESTAMPDIFF(HOUR,submitTime,editTime) as timediff from orders where editTime is not null ;

我这里取了100条数据

MariaDB [mypchelper]>  SELECT id_orders,TIMESTAMPDIFF(HOUR,submitTime,editTime
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值