mysql在时间戳中两个表的重叠 [英] mysql overlaps of two tables in timestamps
问题描述
我在MySQL中有两个表.在这些表的每一个中,保存了具有其MAC地址和状态区域信息的设备.
I have two tables in MySQL. In each of these tables, devices with their MAC-Addresses and information of their states area saved.
这些状态确实在某些时间戳记(startTime,endTime)及其持续时间(endTime-startTime)处开始和结束,并且是由某些ID引起的.
These states do start and end at certain timestamps (startTime,endTime) and their durations (endTime-startTime), and are caused by certain id's.
现在,我想查找某些事件移动"和负载"之间的重叠部分,这些重叠部分以天为单位,如下所示:
Now i want to find the overlappings between the certain events "move" and "load" separated by days, like this:
sum of time, when movement took place (value in the fiddle: 421)
sum of time, when load took place (value in the fiddle: 520)
sum of time, when movement and load took place (value in the fiddle: 391)
查询结果应如下所示:
ID DATE ALL_MOVEMENT ALL_LOAD LOAD_MOVEMENT
00:50:c2:63:10:1a October, 29 2012 00:00:00+0000 421 520 391
00:50:c2:63:10:1a February, 22 2013 00:00:00+0000 421 520 391
00:50:c2:63:10:1b February, 22 2013 00:00:00+0000 181 220 181
我在这里准备了一个小提琴: http://sqlfiddle.com/#!2/c210c
I have prepared a fiddle here: http://sqlfiddle.com/#!2/c210c
更新:可以在这里看到以不同的表结构提供我想要的东西的小提琴:
Update: A fiddle which offers the things i want in a different table structure can be seen here:
http://sqlfiddle.com/#!2/31b94/1
推荐答案
我不太了解数据集与结果集的关系,但在我看来,您或多或少需要这样的东西... >
I don't really understand how the data set correlates with the result set but it seems to me you want something more or less like this...
SELECT m.id
, DATE(FROM_UNIXTIME(m.starttime))Date,SUM(m.duration) all_movement
, SUM(l.duration) all_load
FROM move_table m
JOIN load_table l
ON l.id = m.id
AND l.endtime > m.starttime
AND l.starttime < m.endtime
GROUP
BY m.id
, DATE(FROM_UNIXTIME(m.starttime));
+-------------------+------------+--------------+----------+
| id | Date | all_movement | all_load |
+-------------------+------------+--------------+----------+
| 00:50:c2:63:10:1a | 2012-10-29 | 391 | 520 |
| 00:50:c2:63:10:1a | 2013-02-22 | 391 | 520 |
| 00:50:c2:63:10:1b | 2013-02-22 | 181 | 220 |
+-------------------+------------+--------------+----------+
偶然地,duration
在这种情况下似乎是多余的,因为(例如)SUM(m.endtime-m.starttime)将为您提供相同的结果.
Incidentally, duration
appears to be redundant in this context as (for instance) SUM(m.endtime-m.starttime) will get you the same result.
这篇关于mysql在时间戳中两个表的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!