我正在尝试offset
相同的columns
数据,以便我可以计算它们之间的时间差异。
我从DB
中选择的数据如下所示。
22/12/2015 14:01 22/12/2015 14:01
22/12/2015 14:02 22/12/2015 14:02
04/01/2016 07:48 04/01/2016 07:48
04/01/2016 07:48 04/01/2016 07:48
04/01/2016 07:49 04/01/2016 07:49
04/01/2016 07:49 04/01/2016 07:49
04/01/2016 07:49 04/01/2016 07:49
04/01/2016 07:50 04/01/2016 07:50
04/01/2016 08:00 04/01/2016 08:00
04/01/2016 08:18 04/01/2016 08:18
我需要它看起来像这样 -
22/12/2015 14:01 22/12/2015 14:02
22/12/2015 14:02 04/01/2016 07:48
04/01/2016 07:48 04/01/2016 07:48
04/01/2016 07:48 04/01/2016 07:49
04/01/2016 07:49 04/01/2016 07:49
04/01/2016 07:49 04/01/2016 07:49
04/01/2016 07:49 04/01/2016 07:50
04/01/2016 07:50 04/01/2016 08:00
04/01/2016 08:00 04/01/2016 08:18
04/01/2016 08:18 04/01/2016 08:20
正如您所看到的,我只是删除了第二列的第一行,以便它们现在不同,然后可以计算得到差异。
如果您可以使用Integer键实际访问它,我可以在我的while循环中轻松解决这个问题 -
$results = CDB::ExecuteQuery($SQL);
$firstMovementCounter = 0; // Start counter as normal
$secondMovementCounter = 1; // Start offset to first movement to calculate difference between movements.
while ($rows = CDB::GetAssoc($results))
{
$differenceInSeconds = $rows["FIRST_MOVEMENT"][$firstMovementCounter] - $rows["SECOND_MOVEMENT"][$secondMovementCounter];
$firstMovementCounter++;
$secondMovementCounter++;
}
但这似乎不起作用。还有另一种方法可以成功使用这种方法吗?
为了深入研究,我把它放到调试中,看到在结果的第一个循环中$ rows变量=以下 -
array(2) (
[FIRST_MOVEMENT] => (string) 2015-12-22 14:01:15
[SECOND_MOVEMENT] => (string) 2015-12-22 14:01:15
)
而且$rows["FIRST_MOVEMENT"][$firstMovementCounter]
= = 1
哪个没意义
另一种方法是能够忽略右手first row
的{{1}},而无需跳过第一个column
中的first row
。但我无法轻易解决这个问题。
有没有人能说明我想做什么?
答案 0 :(得分:1)
我认为你应该尝试以下代码:
$results = CDB::ExecuteQuery($SQL);
$firstMovementCounter = 0;
$secondMovementCounter = 1;
while ($rows = CDB::GetAssoc($results))
{
$allrows[] = $rows;
}
for($i = 0; $i < count($allrows); $i++) {
$differenceInSeconds = $allrows[$firstMovementCounter]['FIRST_MOVEMENT'] - $allrows[$secondMovementCounter]['SECOND_MOVEMENT'];
$firstMovementCounter++;
$secondMovementCounter++;
}
这里是最后一个数组键,你必须添加条件。
答案 1 :(得分:0)
您是否考虑过在数据库中执行此操作?例如,在MySQL中,您可以查询同一个表两次,并将日期字段拉出两次。
SELECT
A.requestid,
A.dateime AS starttime,
B.dateime AS endtime
FROM
MyTable A
INNER JOIN
MyTable B ON B.requestid = (A.requestid + 1)
ORDER BY
A.requestid ASC
这可以为您提供您正在寻找的结果集,而无需在PHP中处理结果
+-----------+------------------+------------------+
| requestid | starttime | endtime |
+-----------+------------------+------------------+
| 1 | 22/12/2015 14:01 | 22/12/2015 14:02 |
| 2 | 22/12/2015 14:02 | 04/01/2016 07:48 |
| 3 | 04/01/2016 07:48 | 04/01/2016 07:48 |
| 4 | 04/01/2016 07:48 | 04/01/2016 07:49 |
| 5 | 04/01/2016 07:49 | 04/01/2016 07:49 |
| 6 | 04/01/2016 07:49 | 04/01/2016 07:49 |
| 7 | 04/01/2016 07:49 | 04/01/2016 07:50 |
| 8 | 04/01/2016 07:50 | 04/01/2016 08:00 |
| 9 | 04/01/2016 08:00 | 04/01/2016 08:18 |
| 10 | 04/01/2016 08:18 | 04/01/2016 08:20 |
+-----------+------------------+------------------+