如何从php中的两个连续行中找到两个字段的差异


How to find difference of two fields from two consecutive rows in php?

我有一个机器的mysql日志表,格式如下。

     ---------------------------------------------------------------------------------------------------
    | Event_Id | Event_Type | Machine_No | Operator  | Time Stamp          | Shift | Reason     | Count |
     ---------------------------------------------------------------------------------------------------
    | 101      | Up         | Machine1   | operator1 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 102      | Up         | Machine2   | operator2 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 103      | Up         | Machine3   | operator3 | 2012-06-09 01:03:55 | S1    | Start      | 1     |
    | 104      | Down       | Machine1   | operator1 | 2012-06-09 02:03:55 | S1    | Break Down | 1     |
    | 101      | Up         | Machine1   | operator1 | 2012-06-09 02:33:55 | S1    | After BD   | 1     |
     ---------------------------------------------------------------------------------------------------

桌子就这样摆着。

通过传递以下查询。

$data = mysql_query("SELECT * FROM rpt_machine_log WHERE machine='machine1' AND shift='Shift1'")

我能够得到以下输出。

 101    Up      machine1    operator1   2012-06-09 01:03:55 Shift1  Start of The Shift  1
 106    Down    machine1    operator1   2012-06-09 03:15:55 Shift1  Break               1
 109    Up      machine1    operator1   2012-06-09 03:30:55 Shift1  After The Break     1
 112    Down    machine1    operator1   2012-06-09 03:45:55 Shift1  Break Down          1
 115    Up      machine1    operator1   2012-06-09 05:00:55 Shift1  After Break Down    1
 116    Down    machine1    operator1   2012-06-09 05:30:55 Shift1  Break Down          2
 117    Up      machine1    operator1   2012-06-09 05:45:55 Shift1  After Break Down    2
 118    Down    machine1    operator1   2012-06-09 06:00:55 Shift1  End of Shift        1

现在我想在php代码中找到每个连续的Up和Down时间的差异。

我还想将shift2添加到同一查询中,以显示shift1&2.

由于我是php的新手,所以我没能解决这个问题。

有人能帮我吗。

为了获得shift2,我会做:

SELECT * 
FROM rpt_machine_log 
WHERE machine='machine1' AND (shift='Shift1' OR shift='Shift2')
ORDER BY shift,`Time Stamp`

对于具有空格或特殊字符的列,您需要在其周围添加"。老实说,对所有列都这样做更安全。某些列(如Add)会导致查询失败,因为Add是sql中的保留关键字。

如果你能保证向上总是向下,那么你可以循环你的结果:while($row=mysql_fetch_assoc($data)){}

在这之后,有很多方法可以去存储数据,可以尝试:

if(isset($timestamp)){
$difftime=strtotime($row['Time Stamp'])-strtotime($timestamp);
$hours=floor($difftime/3600);
$difftime-=$hours*3600;
$minutes=floor($difftime/60);
$difftime-=$minutes*60;
$seconds=$difftime;
$diff_array[]=$hours.":".$minutes.":".$seconds;
unset($timestamp);}
else{
$timestamp=$row['Time Stamp'];}

这里有一个所有时差的数组。

显然,如果你不能保证上升总是在下降之后,或者你想划分shift1和shift2的时间差,那么你就必须在while循环中添加额外的检查。

---编辑---

if(isset($timestamp)){
$difftime=strtotime($row['Time Stamp'])-strtotime($timestamp);
$diff_array[]=$difftime;
unset($timestamp);}
else{
$timestamp=$row['Time Stamp'];}
foreach ($diff_array as &$value){
$Uptime += $value;}
$hours=floor($Uptime/3600);
$Uptime-=$hours*3600;
$minutes=floor($Uptime/60);
$Uptime-=$minutes*60;
$seconds=$convert;
$Uptime=$hours.":".$minutes.":".$seconds;

---EDIT2-

while($row=mysql_fetch_assoc($data)){
$array['event_type'][]=$row['event_type'];
$array['timestamp'][]=$row['Time Stamp'];}

现在您有了一个由两个数组组成的数组:event_types和timestamps。如果您想要的话,也可以将其作为两个不同的数组来执行,例如$event_type[]=$row['event_type'];,并相应地更改其余的数组。

现在,您可以执行for循环来迭代这些结果,并执行所需的检查。

$count=count($array['event_type']);
for($x=0;$x<$count;$x++){
if($row['event_type'][$x]=='Down' && $row['event_type'][$x+1]=='Up'){
}}

请记住在for循环之前计算计数,将其作为条件之一意味着每次都要计算它,因此会产生性能成本。

此外,如果您想跳过mysql结果的第一个结果,只需在while循环之前调用$row=mysql_fetch_assoc($data)一次。

您也可以通过连接回表在MySQL中完全做到这一点。像这样(未经测试)的代码应该可以工作:

SELECT *
FROM rpt_machine_log rpt1, rpt_machine_log rpt2
WHERE rpt1.event_type = "Up"
AND rpt1.machine_no = rpt2.machine_no
AND rpt2.`time stamp` > rpt1.`time stamp`
AND rpt2.event_type = "Down"

有一些角落案例,但这应该让你开始。