我有一个遗留的MariaDB数据库,它以以下方式保存我们的温度和湿度传感器的历史数据
id | 日期 | 值标记 | |
---|---|---|---|
1 | 2021-11-10 08:08:13 | 21.3 | 温度|
2 | 2021-11-10 08:08:13 | 52.7 | 哼声|
3 | 2021-11-10 09:08:13 | 23.3 | 温度|
4 | 2021-11-10 09:08:13 | 57.7 | hum
查询:
select
JSON_ARRAYAGG(
JSON_OBJECT(
'ts', t1.date, 'ts', t2.date, 'temp',
tValue, 'hum', hValue
)
)
from
(
SELECT
date,
value as "tValue"
from
HistoricalData
where
tag = "temp"
) t1
join (
SELECT
date,
value as "hValue"
from
HistoricalData
where
tag = "hum"
) t2 on t1.date = t2.date;
结果:
[
{
"ts":"2021-11-10 08:08:13.000000",
"hum":52.70000076293945,
"temp":21.299999237060547
},
{
"ts":"2021-11-10 09:08:13.000000",
"hum":57.70000076293945,
"temp":23.299999237060547
}
]