Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql选择差异为10分钟的所有行_Php_Mysql - Fatal编程技术网

Php Mysql选择差异为10分钟的所有行

Php Mysql选择差异为10分钟的所有行,php,mysql,Php,Mysql,我有一个带有时间戳的表,我想检索彼此相差10分钟的所有记录。 我举一个例子: 我的数据库中有4个日期: 01-12-2015 15:45:10 01-12-2015 15:55:10 01-12-2015 15:25:10 01-12-2015 15:15:10 我只想检索彼此相差10分钟的两个日期 我不想获取时间戳在当前时间戳10分钟内的所有记录。我想选择所有的记录之间有10分钟的差异。 因此,在我的示例中,我希望选择日期: 01-12-2015 15:45:10 01-12-2015 15

我有一个带有时间戳的表,我想检索彼此相差10分钟的所有记录。 我举一个例子: 我的数据库中有4个日期:

01-12-2015 15:45:10
01-12-2015 15:55:10
01-12-2015 15:25:10
01-12-2015 15:15:10
我只想检索彼此相差10分钟的两个日期

我不想获取时间戳在当前时间戳10分钟内的所有记录。我想选择所有的记录之间有10分钟的差异。 因此,在我的示例中,我希望选择日期:

01-12-2015 15:45:10
01-12-2015 15:55:10
这两个日期之间应相差10分钟。
我该怎么做呢?

下面是一个示例,说明如何计算两个日期在当前时间1st参数中是否相差10分钟,比如说当前时间。 选择TIMEDIFF'2015-12-11 08:20:00'、'2015-12-11 08:10:00'

您可以立即使用以获取当前时间。因此:

select t.*
from t
where t >= date_sub(now(), interval 10 minute);

我希望你有一个索引。在index=index+1>上进行连接->每一行都与前一行连接。然后计算时间差异,并仅选取差异超过10分钟的行

SELECT *
FROM dates AS a LEFT OUTER JOIN dates AS b 
ON a.id = b.id+1
WHERE a.timestamps-b.timestamps >= 600
看看这把小提琴:

试试:

Select * from a where a.id not in
 (select b.id from a b where TIMEstampDIFF(SECOND,a.mydate,b.mydate)<=600)
and a.id not in
 (select c.id from a c where TIMEstampDIFF(SECOND,a.mydate,c.mydate)>600)

您可以从sql查询中获取数据,并在php文件中写入逻辑,如下所示:

$select =   mysql_query("SELECT your_datetime_column_name as time,UNIX_TIMESTAMP(your_datetime_column_name) as time_string FROM your_table_name");
 $result = mysql_fetch_array($select);
    $final_result= NULL;
    $j=0;
    foreach($result as $ff)
    {
       foreach($result as $ff11)
    {
       if(abs($ff11['time_string'] - $ff['time_string']) ==600 ){ 
       $final_result[$j]['time1']=$ff['time'];
           $final_result[$j]['time2']=$ff11['time'];
       $j++;
        }
    }
    }
在数组$final_result中,您将获得10分钟差的所有日期对。
希望它能对您有所帮助。

请显示您的查询可能的重复项否它不是重复项。我不想获取时间戳在当前时间戳10分钟内的所有记录。我想选择它们之间相差10分钟的所有记录。为什么它看起来不像一个时间戳?我的代码:选择*FROM stats AS a LEFT OUTER JOIN stats AS b ON a.id=b.id+1,其中a.time-b.time=100;但是我得到了错误SQLSTATE[22003]:数值超出范围:1690 BIGINT UNSIGNED value在“panel.a.time-panel.b.time”中超出范围您必须将日期从字符串格式转换为unixtimestamp格式,以便简单地计算差异。我认为您应该看看MySQL的日期时间函数,特别是UNIX_TIMESTAMP,就像我的SQL小提琴一样