【发布时间】:2014-10-13 16:44:43
【问题描述】:
我有两张表 Fuel 和 DrivingTime。 Fuel 有 accountID、deviceID、timestamp、fuelLevel、address。 DrivingTime 有 startTime、stopTime。如果时间戳与 startTime 或 stopTime 匹配,我的目标是显示 Fuel 中的所有字段。我这样写了查询:
SELECT F.deviceID, F.timestamp, F.fuelLevel,F.address
FROM Fuel F, DrivingTime DT
where (F.timestamp = DT.stopTime or F.timestamp = DT.startTime)
and F.accountID = 'something1' and F.deviceID = 'something2';
不幸的是,我发现Fuel中没有任何时间戳与DrivingTime中的startTime匹配,只有F.timestamp = DT.stopTime返回true。搜索谷歌后,我可以分别将时间戳与 startTime 和 stopTime 匹配,但是我将它们匹配在一起感到困惑。代码如下:
select from_unixtime(F.timestamp), F.fuelLevel, F.address
from gtse.tblFuel F, gtse.tblDrivingTime DT
where DT.stopTime = F.timestamp and F.accountID = 'vinhnghia'
and F.deviceID = '14C-00027'
这里:
select from_unixtime(F.timestamp), F.fuelLevel, F.address
from gtse.tblFuel F, gtse.tblDrivingTime D
where D.accountID = 'vinhnghia' and D.deviceID = '14C-00027'
and F.timestamp between D.startTime and D.startTime + '60'
order by
abs(F.timestamp - D.startTime) asc limit 1
。那么如何在一个查询中匹配以上两个代码呢?
【问题讨论】:
-
你需要通过 timestampdiff 函数在两者之间使用连接