我试图在行之间找时间差异。我试过了,但它没有用。
SELECT id,locationDate,
TIMESTAMPDIFF(SECOND,
(SELECT MAX(locationDate) FROM location WHERE locationDate< t.locationDate),
created_at
) as secdiff
FROM location t where tagCode = 24414 AND locationDate >= '2017-05-10 16:00:01' and locationDate <= '2017-05-10 16:59:59';
如何计算行间的时差?
您可以从sqlfiddle
获取样本结构和数据答案 0 :(得分:1)
我猜你只想要一个相关的子查询:
select l.id, l.locationDate,
TIMESTAMPDIFF(SECOND,
(SELECT MAX(l2.locationDate)
FROM location l2
WHERE l2.locationDate < l.locationDate AND
l2.tagCode = l.tagCode
),
locationDate
) as secdiff
from location l
where l.tagCode = 24414 and
l.locationDate > '2017-05-10 16:00:00' and
l.locationDate < '2017-05-10 17:00:00';
我将日期/时间常数修改为更合理(从我的角度来看)。如果您真的关心一段时间之前或之后的一秒钟,那么您可以使用原始配方。