0

我写了一个查询

(select meterID, timestamp from meter_data 
          where timestamp between 1369282639 AND 1369282699 
                AND deviceID = "1" 
                        GROUP BY meterID) 
UNION 

(select meterID, timestamp from meter_data 
          where timestamp between 1369282739 AND 1369282799 
                AND deviceID = "1" 
                         GROUP BY meterID);

我希望它有 2 组数据 - 我的表中有 4 个不同的meterID,所以它应该返回 8 条记录,但它只返回 4 条记录

4

1 回答 1

2

为了帮助理解数据,您可以尝试以下方法

SELECT a.meterID, MAX(b.timestamp), MAX(c.timestamp)
FROM meter_data a
LEFT OUTER JOIN meter_data b ON a.meterID = b.meterID AND b.timestamp between 1369282639 AND 1369282699 AND b.deviceID = "1" 
LEFT OUTER JOIN meter_data c ON a.meterID = c.meterID AND c.timestamp between 1369282739 AND 1369282799 AND c.deviceID = "1" 
GROUP BY a.meterID

这应该为每个仪表 ID 返回一行,以及该仪表 ID 在每个范围内的最大时间戳。

于 2013-06-17T11:04:06.667 回答