0
我有2個表格:交易(a)和價格(b)。我想從表b中檢索在交易日期有效的價格。MAX在左側加入(日期)
表一個包含物品交易的歷史: 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
它工作得很好,但它似乎需要很長的時間,因爲雙子查詢。 同樣可以用左加入完成嗎?
這是不是一個好主意,有一個名爲'日期',因爲這是一個字已被用於sql – Hearner
這是一個例子...數據庫中沒有名爲日期的字段。 –
顯示您擁有的索引,併爲每個表使用「show create table xxx」。哎呀,這將顯示真正的列名,對不起。 – Drew