简体   繁体   English

获取所有唯一行之间的平均差

[英]Get average difference between all unique rows

I have a bantracker which logs to MySQL each ban, I would like to get the average difference between each unix time so I can estimate based on the data when the next ban will happen. 我有一个bantracker,它将每个禁令记录到MySQL,我想获取每个Unix时间之间的平均差,以便我可以根据数据估计下一次禁令发生的时间。

My thinking would be Last entry + average? 我的想法是最后输入+平均? Maybe I am completely wrong and somebody has a better idea. 也许我完全错了,有人有更好的主意。

The Table structure looks like this: 表结构如下所示: 在此处输入图片说明

Each apikey is unique. 每个apikey都是唯一的。

Any help / ideas are much appreciated I don't mind if it's a MySQL query or PHP code. 非常感谢任何帮助/想法,我不介意它是MySQL查询还是PHP代码。

Thanks! 谢谢!

You're trying to calculate a moving average: 您正在尝试计算移动平均线:

function moving_average( $array ) {

  $z = sizeof( $array );

  for ( $i = 1; $i < $z; $i++ ) {
    $result[] = $array[ $i ] - $array[ $i-1 ];
  }

  return array_sum( $result ) / count( $result );

}

Hat tip to: Calculating the average increase from array values in PHP 提示: 计算PHP中数组值的平均增加量

You can use(COUNT,SUM) to find average 您可以使用(COUNT,SUM)求平均值

$sql = "SELECT COUNT(time) as count,SUM(time) as sum,MAX(time) as sum FROM table_name";

$avg=$row["sum"]/$row["count"];

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

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