我有一個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