Mysql的其他领域

问题描述:

我有我的第一台价格为所有文章与定义日期,变革的价格第一次约会的演变两个日期之间..“” evolution_price'Mysql的其他领域

article -- date ----- unit.price 
1 --- 2012-01-01 -- 1.20 
1 --- 2013-01-01 -- 1.10 
1 --- 2014-01-01 -- 1.20 
2 --- 2012-01-01 -- 1.40 
2 --- 2013-01-01 -- 1.50 
2 --- 2014-01-01 -- 1.20 

上订单历史记录(忘了单价0.00)“”订单“”

article-- date---- price unit --- total order 
1 ----- 2012-02-03 -- 0.00 -------- 12.10 
2 ----- 2012-02-05 -- 0.00 -------- 15.20 
1 ----- 2013-01-01 -- 0.00 -------- xxx 
1 ----- 2014-05-05 -- 0.00 
1 ----- 2014-05-06 -- 0.00 

我找我的第二个表格清单当然包括在第二台合适的单价,但伊盖尔两权日之间..的演变价格可以是20日期/价格价格必须介于(日期1至日期2)价格和(2日至3日)价格之间。等等。
第二台所需的'订单 '':

1 ----- 2012-02-03 -- 1.20 ----- 12.10 
2 ----- 2012-02-05 -- 1.20 ----- 15.20 
1 ----- 2013-01-01 -- 1.10 ----- xxx 

我考这一个

update orders set orders.unit_price = ( select unit_price from evolution_price where orders.article_id = evolution_price.article_id and orders.order_date >= evolution_price.change_date order by evolution_price.change_date desc limit 1 );

+0

从Turophile的答案是我搜索...我现在搜索如何直接更新第二张表更新和设置 – Didou70 2015-03-05 09:25:59

欢迎的*!

我想你想是这样的:

select 
    article_id , 
    order_date , 
    unit_price as old_unit_price, 
    order_total, 
    (select unit_price 
    from evolution as e 
    where o.article_id = e.article_id 
    and change_date <= order_date 
    order by change_date desc 
    limit 1) as new_unit_price 
from orders as o 

这是工作在一个小提琴:http://sqlfiddle.com/#!2/414e8/3

请注意,你的问题应该被标记为sql,而不是标签betweentabledata

你发布样本数据和结果真的很棒 - 这真的很有帮助。但有时它也有助于建立你的表格结构。你真的应该自己尝试一个解决方案,并发布它,所以我们可以为你纠正它。

+0

是的感谢...太简单了...但对于新手...这是真的...可以我从这个结果(new_unit_price)直接更新到old_unit_price ..很好的答案...我已经从我的服务器结果1'000'000订单和20'000价格有点反应时间 – Didou70 2015-03-05 09:16:02