简体   繁体   English

在MySQL中使用FULL OUTER JOIN合并和展平表

[英]Combine and flatten tables with a FULL OUTER JOIN in MySQL

I have two tables of price updates from two sources (A and B) in the following format. 我有两个价格来源的两个价格更新表(A和B),格式如下。 These are price updates for Source A to the millisecond: 这些是源A的毫秒级价格更新:

index,timestamp,origin,product,bid,ask,nexttimestamp
A1,2016-10-18 20:39:25.595,A,Dow,7005.5,7007.5,2016-10-18 20:39:34.701
A2,2016-10-18 20:39:34.701,A,Dow,7005.8,7007.8,2016-10-18 20:39:35.703
A3,2016-10-18 20:39:35.703,A,Dow,7005.5,7007.5,2016-10-18 20:39:38.432
A4,2016-10-18 20:39:38.432,A,Dow,7005.8,7007.8,2016-10-18 20:39:44.055
A5,2016-10-18 20:39:44.055,A,Dow,7005.3,7007.3,2016-10-18 20:39:45.146

These are price updates for Source B to the millisecond: 这些是来源B的毫秒更新价格:

index,timestamp,origin,product,bid,ask,nexttimestamp
B1,2016-10-18 20:39:21.694,B,Dow,7005.8,7006.8,2016-10-18 20:39:27.987
B2,2016-10-18 20:39:27.987,B,Dow,7005.9,7006.9,2016-10-18 20:39:28.058
B3,2016-10-18 20:39:28.058,B,Dow,7006.0,7007.0,2016-10-18 20:39:39.792
B4,2016-10-18 20:39:39.792,B,Dow,7006.2,7007.2,2016-10-18 20:39:39.868
B5,2016-10-18 20:39:39.868,B,Dow,7006.3,7007.3,2016-10-18 20:39:45.845

I've marked an index (A1,B1 etc) in the first column. 我在第一列中标记了索引(A1,B1等)。 I need to combine and flatten these tables, so that I can see the prevailing price for each source, as updates for the other arrive. 我需要合并和展平这些表,以便我可以看到每个来源的现行价格,以及其他来源的更新。 The fields 'timestamp' and 'nexttimestamp' are basically the starttime and endtime for the validity of the price update. 字段“时间戳”和“下一个时间戳”基本上是价格更新有效期的开始时间和结束时间。 The field nexttimestamp is creating by finding the next price update for the same source in the table. 通过在表中查找同一来源的下一个价格更新来创建nexttimestamp字段。

I need to join Tables A and B where the timestamp in B is greater than the timestamp in A and less than the 'nexttimestamp' in A ie if the price update is valid. 我需要加入表A和B,其中B中的时间戳大于A中的时间戳而小于A中的“ nexttimestamp”,即价格更新是否有效。 I need to do a join that shows something like this: 我需要进行显示如下所示的联接:

NULL,                                                                  B1,2016-10-18 20:39:21.694,B,Dow,7005.8,7006.8,2016-10-18 20:39:27.987
A1,2016-10-18 20:39:25.595,A,Dow,7005.5,7007.5,2016-10-18 20:39:34.701,B1,2016-10-18 20:39:21.694,B,Dow,7005.8,7006.8,2016-10-18 20:39:27.987
A1,2016-10-18 20:39:25.595,A,Dow,7005.5,7007.5,2016-10-18 20:39:34.701,B2,2016-10-18 20:39:27.987,B,Dow,7005.9,7006.9,2016-10-18 20:39:28.058
A1,2016-10-18 20:39:25.595,A,Dow,7005.5,7007.5,2016-10-18 20:39:34.701,B3,2016-10-18 20:39:28.058,B,Dow,7006.0,7007.0,2016-10-18 20:39:39.792
A2,2016-10-18 20:39:34.701,A,Dow,7005.8,7007.8,2016-10-18 20:39:35.703,B3,2016-10-18 20:39:28.058,B,Dow,7006.0,7007.0,2016-10-18 20:39:39.792
A3,2016-10-18 20:39:35.703,A,Dow,7005.5,7007.5,2016-10-18 20:39:38.432,B3,2016-10-18 20:39:28.058,B,Dow,7006.0,7007.0,2016-10-18 20:39:39.792
A4,2016-10-18 20:39:38.432,A,Dow,7005.8,7007.8,2016-10-18 20:39:44.055,B3,2016-10-18 20:39:28.058,B,Dow,7006.0,7007.0,2016-10-18 20:39:39.792
A4,2016-10-18 20:39:38.432,A,Dow,7005.8,7007.8,2016-10-18 20:39:44.055,B4,2016-10-18 20:39:39.792,B,Dow,7006.2,7007.2,2016-10-18 20:39:39.868
A4,2016-10-18 20:39:38.432,A,Dow,7005.8,7007.8,2016-10-18 20:39:44.055,B5,2016-10-18 20:39:39.868,B,Dow,7006.3,7007.3,2016-10-18 20:39:45.845
A5,2016-10-18 20:39:44.055,A,Dow,7005.3,7007.3,2016-10-18 20:39:45.146,NULL

I have been trying queries such as the following, but to no avail. 我一直在尝试如下查询,但无济于事。

   select main.*, sub.*
    from test as main
    left join test as sub on sub.timestamp > main.timestamp and sub.timestamp < main.nexttimestamp  and sub.origin <> main.origin and sub.product = main.product
    order by main.timestamp ;

You can build a FULL OUTER JOIN in MySQL combining LEFT JOIN and RIGHT JOIN with UNION: 您可以在MySQL中将LEFT JOIN和RIGHT JOIN与UNION结合起来建立FULL OUTER JOIN:

select a.*, b.*
from table_a a
left join table_b b 
  on  b.timestamp > a.timestamp
  and b.timestamp < a.nexttimestamp

union all

select a.*, b.*
from table_a a
right join table_b b 
  on  b.timestamp > a.timestamp
  and b.timestamp < a.nexttimestamp
where a.index is null

In the second part (RIGHT JOIN) you need the IS NULL condition, because all other rows are already included in the first part (LEFT JOIN). 在第二部分(RIGHT JOIN)中,您需要IS NULL条件,因为所有其他行都已包含在第一部分(LEFT JOIN)中。

You can add more conditions in the ON clause if needed (like b.product = a.product ). 如果需要,可以在ON子句中添加更多条件(例如b.product = a.product )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM