【发布时间】:2011-03-06 13:05:06
【问题描述】:
我有两个想要加入的 MySQL 表(产品和价格历史记录):
Product表:
Id = int
Name = varchar
Manufacturer = varchar
UPC = varchar
Date_added = datetime
Price_h表:
Id = int
Product_id = int
Price = int
Date = datetime
我可以执行一个简单的 LEFT JOIN:
SELECT Product.UPC, Product.Name, Price_h.Price, Price_h.Date
FROM Product
LEFT JOIN Price_h
ON Product.Id = Price_h.Product_id;
但正如预期的那样,如果我在价格历史表中有多个产品条目,我会为每个历史价格获得一个结果。
如何构造一个只返回每个产品的一个实例的连接结构,其中只连接价格历史表中的最新条目?
【问题讨论】:
标签: mysql join distinct left-join subquery