获取MySQL中join表的最后值



对于家庭自动化仪表板,我试图显示不同房间中不同设备的最新值。我的桌子是这样的表:设备

测量tbody> <<tr>217
DeviceID 地板 房间更多信息
10WCRoom-Temp
0WC所需Room-Temp
30WCrel。湿度
151孩子Room-Temp
161孩子所需Room-Temp
1孩子rel。湿度

在MySQL 8+上,我们可以使用ROW_NUMBER():

WITH cte AS (
SELECT d.DeviceID, d.Floor, d.Room, d.Measurement, m.Timestamp, m.Value,
ROW_NUMBER() OVER (PARTITION BY d.DeviceID ORDER BY m.Timestamp DESC) rn
FROM devices d
LEFT JOIN messdaten m ON m.DeviceID = d.DeviceID 
WHERE Room IN (SELECT Room FROM devices WHERE DeviceID = 1) 
)
SELECT DeviceID, Floor, Room, Measurement, Timestamp, Value
FROM cte
WHERE rn = 1
ORDER BY Measurement;

相关内容

  • 没有找到相关文章

最新更新