我正在尝试从两个不同的表中获取结果,如下所示:
|TEMPERATURETAB|
时间戳 | 温度 |
---|---|
时间1 | 价值T1 |
时间2 | 价值T2 |
|HUMIDITYTAB|
时间戳 | 湿度 |
---|---|
时间3 | 价值H1 |
时间4 | 价值H2 |
表之间没有联系,时间戳略有不同,所以我认为我不应该使用JOIN
我尝试使用 UNION (因为我不需要湿度表中的时间戳,当我在 php 中获取它时我会删除它)
SELECT
Timestamp as 'Time',
Temperature as 'Temp'
FROM Temperaturetab
WHERE Timestamp BETWEEN '[some timestamp]' AND '[some other timestamp]'
UNION
SELECT
Timestamp as 'TimeB',
Humidity as 'Hum'
FROM Humiditytab
WHERE Timestamp BETWEEN '[some timestamp]' AND '[some other timestamp]'
它只返回 时间和温度
您应该将温度和湿度放在不同的列中。对另一个表的列使用虚拟值。
SELECT
Timestamp as 'Time',
Temperature as 'Temp'
NULL AS 'Hum'
FROM Temperaturetab
WHERE Timestamp BETWEEN '[some timestamp]' AND '[some other timestamp]'
UNION
SELECT
Timestamp as 'Time',
NULL AS Temp
Humidity as 'Hum'
FROM Humiditytab
WHERE Timestamp BETWEEN '[some timestamp]' AND '[some other timestamp]'
温度行的湿度列中将为 NULL,反之亦然。