我有一张名为water_tank的表,
select * from tank_id;
id unique_tankId highLevel lowLevel
1 1 75 50
2 2 80 55
select * from water_tank where tabkId=1;
id tankId level serverTimeStamp
1 1 50 2016-06-28 12:00:01
2 1 60 2016-06-28 12:59:01
3 1 85 2016-06-28 13:00:01
4 1 89 2016-06-28 13:30:01
5 1 90 2016-06-28 14:00:01
6 1 100 2016-06-28 15:00:01
7 1 10 2016-06-28 16:00:01
8 1 20 2016-06-28 16:30:01
9 1 28 2016-06-28 17:00:01
10 1 50 2016-06-29 12:00:01
11 1 60 2016-06-29 12:59:01
12 1 67 2016-06-29 13:00:01
13 1 75 2016-06-29 15:00:01
14 1 90 2016-06-29 16:00:01
15 1 95 2016-06-29 17:00:01
16 1 98 2016-06-29 18:00:01
17 1 100 2016-06-29 19:30:01
18 1 24 2016-06-29 20:00:01
.. .. .. ...
.. .. .. ...
注意: id是AI,tankId是外键
我希望使用mysql查询找到从high(level> = 75)级别serverTimeStamp到low(level< 50)级别serverTimeStamp的时间差。
期待输出:
timeDiffInHour highReachedTime cleanedTime
3 2016-06-28 13:00:01 2016-06-28 16:00:01
5 2016-06-29 15:30:01 2016-06-29 20:00:01
.. ... ...
有人帮助我实现这个目标吗?
答案 0 :(得分:0)
也许是这样的?
SELECT tankId,
MIN(serverTimeStamp) lowReachedTime,
b.highReachedTime,
TIMESTAMPDIFF(HOUR,MIN(serverTimeStamp),b.highReachedTime) timeDiffinHour
FROM tankId a
INNER JOIN (
SELECT tankId, MAX(serverTimeStamp) highReachedTime
FROM water_tank
WHERE level > 75
GROUP BY tankId
) b ON b.tankId = a.tankId
WHERE level < 50
GROUP BY a.tankId