【问题标题】:Mysql between two date from other fields来自其他字段的两个日期之间的Mysql
【发布时间】:2015-03-03 09:50:07
【问题描述】:

我的第一张桌子上有所有文章价格的演变,第一个日期定义了价格变化的日期。''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)''orders''

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 日期/价格,价格必须介于 (date1 到 date2)price 和 (2date to 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 );

【问题讨论】:

  • Turophile 的答案是我搜索的内容...我现在搜索如何使用 update 和 set 直接更新第二个表

标签: mysql


【解决方案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

它正在发挥作用:http://sqlfiddle.com/#!2/414e8/3

请注意,您的问题应标记为sql,而不是标记为betweentabledata

很高兴您发布了示例数据和结果 - 这真的很有帮助。但有时它也有助于拥有您的表结构。您确实应该自己尝试解决方案并将其发布,以便我们为您更正。

【讨论】:

  • 是的,谢谢...太简单了...但是对于新手...这是真实的...我可以使用从这个结果 (new_unit_price) 到 old_unit_price 的集合直接更新.. 很好的答案。 .. 我的服务器结果 1'000'000 个订单和 20'000 个价格只有一点响应时间
猜你喜欢
  • 2023-02-07
  • 1970-01-01
  • 2013-09-16
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
相关资源
最近更新 更多