【发布时间】:2016-06-11 00:13:55
【问题描述】:
我有 2 个表:交易 (a) 和价格 (b)。我想从表 b 中检索在交易日期有效的价格。
表 a 包含文章交易的历史记录: Store_type、日期、文章、...
表 b 包含商品价格的历史记录: Store_type、日期、文章、价格
目前我有这个:
Select
a.Store_type,
a.Date
a.Article,
(select b.price
from PRICES b
where b.Store_type = a.Store_type
and b.Article = a.Article
and b.Date = (select max(c.date)
from PRICES c
where c.Store_type = a.Store_type
and c.Article = a.Article
and c.date <= a.date)) AS ART_PRICE
from TRANSACTIONS a
它工作得很好,但由于双子查询,它似乎需要很长时间。 可以用 LEFT JOIN 做同样的事情吗?
【问题讨论】:
-
拥有一个名为
Date的字段不是一个好主意,因为这是一个已经在sql中使用的词 -
这是一个例子......数据库中没有名为date的字段。
-
显示你拥有的索引,每个表都有一个
show create table xxx。糟糕,这将显示真实的列名,抱歉。