我的第一个表格上有所有文章的价格演变,第一个日期定义了价格更改的日期..''evolution_price''
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
我测试了这个
更新订单
设置订单.unit_price =
(
选择
单价
来自进化价格
在哪里
order.article_id = evolution_price.article_id
和
orders.order_date >=development_price.change_date
命令
按evolution_price.change_date desc limit 1
);
最佳答案
欢迎来到 StackOverflow!
我想你想要这样的东西:
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
这里正在 fiddle 中工作:http://sqlfiddle.com/#!2/414e8/3
请注意,您的问题应标记为 sql
,而不是标记为 Between
、table
或 data
。
很高兴您发布了示例数据和结果 - 这确实很有帮助。但有时它也有助于你的表结构。您确实应该自己尝试解决方案并将其发布,以便我们可以为您纠正。
关于Mysql 之间来自其他字段的两个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28828638/