我有一个机器的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 |
---------------------------------------------------------------------------------------------------
table 是这样的。
通过传递以下查询。
$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 添加到同一查询中,以显示类次 1 和 2 的 machine1 日志。
由于我是 php 新手,所以无法解决这个问题。
谁能帮帮我。
最佳答案
要获得shift2,我也会这样做:
SELECT *
FROM rpt_machine_log
WHERE machine='machine1' AND (shift='Shift1' OR shift='Shift2')
ORDER BY shift,`Time Stamp`
对于包含空格或特殊字符的列,您希望在它们周围添加`。老实说,对所有列都这样做更安全。某些列(例如 Add)会导致查询失败,因为 add 是 sql 中的保留关键字。
如果你能保证 up 总是跟随 down 那么你可以循环你的结果:
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'];}
这里有一个包含所有时差的数组。
显然,如果您不能保证 up 总是在 down 之后出现,或者您想分割 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;
---编辑2---
while($row=mysql_fetch_assoc($data)){
$array['event_type'][]=$row['event_type'];
$array['timestamp'][]=$row['Time Stamp'];}
现在您有一个由两个数组组成的数组:event_types 和时间戳。如果您想要,您也可以将其作为两个不同的数组来执行$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 结果的第一个结果,只需调用 $row=mysql_fetch_assoc($data)
在 while 循环之前一次。
关于php - 如何在PHP中查找两个连续行中两个字段的差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12290406/