2015-09-25 56 views
1

我有两个表是这样的:合并具有不同的时间戳两个表一个查询输出

表1:

id timestamp    Volume value 
    1 2015-02-17 14:11:10  1220.62  0 
    2 2015-02-17 14:13:48  1220.62  0 
    3 2015-02-17 14:16:39  1220.62  0 
    4 2015-02-17 14:17:22  1220.62  0 
    5 2015-02-17 14:17:47  1220.62  0 

表2:

id TimeDate aussentemp 
    1 2015-02-17 14:11:15  0 
    2 2015-02-17 14:13:03  22.9 
    3 2015-02-17 14:16:04  23 
    4 2015-02-17 14:17:02  22.9 
    5 2015-02-17 14:17:03  23 

你可以看到时间戳几乎相同。只是想合并它,SQL查询使用表1中的时间戳和数据,并添加一个新的列,其中包含几乎相同时间戳的“aussentemp”。

有人可以帮助我吗?

回答

1

下面是一个使用相关子查询的一种方法:

select t1.*, 
     (select t2.aussentemp 
     from table2 t2 
     where t2.timedate <= t1.timedate 
     order by t2.timedate desc 
     limit 1 
     ) as aussentemp 
from table1 t1; 

注:这需要或在表1中的时间戳之前的最新值。

如果您还需要时间戳,可以重复子查询。

相关问题