我有一个以下格式的mysql表
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 101 | Up | Machine1 | operator1 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 102 | Up | Machine2 | operator2 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 103 | Up | Machine3 | operator3 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 104 | Down | Machine1 | operator1 | 2012-09-06 02:03:55 | Shift1 | Break Down | 1 |
| 101 | Up | Machine1 | operator1 | 2012-09-06 02:33:55 | Shift1 | After BD | 1 |
-------------------------------------------------------------------------------------------------------------
通过传递以下查询。
$data = mysql_query("select * from rpt_machine_log where (timestamp between date_sub(now(),INTERVAL 2 WEEK) and now() && (machine='machine$i') && (shift='Shift1') ) AND (reason='Start of The Shift' OR reason='End of Shift') ORDER BY 'even_id'")
我得到以下输出
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 101 | Up | Machine1 | operator1 | 2012-09-06 01:03:55 | Shift1 | Start of The shift | 1 |
| 118 | Down | Machine1 | operator1 | 2012-09-06 06:00:55 | Shift1 | End of The shift | 1 |
| 127 | Up | Machine1 | operator1 | 2012-09-07 01:03:55 | Shift1 | Start of The shift | 1 |
| 148 | Down | Machine1 | operator1 | 2012-09-07 02:03:55 | Shift1 | End of The shift | 1 |
-------------------------------------------------------------------------------------------------------------
我通过找出班次结束和班次之星的差异来计算总班次时间,如下面的代码。
while ($row = mysql_fetch_assoc($shifttime{$i}))
{
if(isset($timestamp))
{
$diff_shift_time{$i}= strtotime($row['timestamp']) - strtotime($timestamp);
$diff_shift_time_array{$i}[]=$diff_shift_time{$i};
unset($timestamp);
}
else
{
$timestamp=$row['timestamp'];
}
}
foreach ($diff_shift_time_array{$i} as &$value){
$Total_shift_time{$i} += $value;
}
在某些情况下,查询的输出如下。
-------------------------------------------------------------------------------------------------------------
| event_id | event_type | machine | user | timeStamp | Shift | Reason | Count |
-------------------------------------------------------------------------------------------------------------
| 121 | Up | Machine1 | operator1 | 2012-09-06 06:03:55 | Shift2 | Start of The shift | 1 |
| 124 | Down | Machine1 | operator1 | 2012-09-06 01:00:55 | Shift2 | End of The shift | 1 |
| 151 | Up | Machine1 | operator1 | 2012-09-07 06:03:55 | Shift2 | Start of The shift | 1 |
-------------------------------------------------------------------------------------------------------------
当我计算总班次时间时,我得到负值,因为我的结果表以 Up 事件结束。
由于计算是连续的 up 和 down( so the no of rows of the resultant row should always be even
),所以最后的 up 没有 down 事件来执行计算。
如果行数是奇数,我想删除表的最后一行。
我怎样才能实现它。