我有一个MySQL数据库,其中包含3个表格,定义如下。MySQL基于时间戳的IF选择
Readings: (_id, value, timestamp, sensorId)
Sensor: (_id)
DeviceSensor: (deviceId, sensorId, timestamp)
Reading
每个具有收集数据时的时间戳。 Sensor
是Reading
的所有者。 DeviceSensor
包含Device
和Sensor
之间的关系,包括关系开始时的时间戳。
在我的建筑,当Device
与Sensor
配对时,Sensor
忽略前者Devices
。
我需要的是查询所有Readings
并在每次阅读时将它们与相应的Device
相关联。例如:
DeviceSensor = [(1,1,1483527932),(1,2,1507058076)]
Sensor = [(1)]
Readings = [(1,5,1483527932,1),(2,3,1493527932,1),(3,7,1507058076,1)]
有了这些数据,我的查询将返回:[(1,5,1483527932,1),(2,3,1493527932,1),(3,7,1507058076,2)]
。也就是说,Readings
有timestamp
1483527932
和1507058076
之间应该返回sensorId = 1
和Readings
1507058076
应该返回sensorId = 2
。
您需要DeviceSensor上的结束时间戳。任何修改表格的机会? – MikeAinOz
如果'Readings'有一个'sensorId'列,这会更简单。 –
@MikeAinOz这是可能的,但它会使表格大小增加一倍以“无”。如果只有这样,那是可行的。 – Minoru