有两个表,A表是产品详情,B表的该产品各类型的价格。我想获得该产品最低的价格和该型号的ID。
A表【Product】:
--------------------------------
id | name
--------------------------------
3 | 联想电脑
--------------------------------
B表【ProductPrice】:
--------------------------------------------------------------------
id | ProductId | xh | price
--------------------------------------------------------------------
1 | 3 | xb110 | 2999
--------------------------------------------------------------------
2 | 3 | xb220 | 3999
--------------------------------------------------------------------
希望查询出最低的那个型号信息:
Product.id | Product.name | ProductPrice.id | ProductPrice.xh | ProductPrice.price
------解决思路----------------------
select * from ProductPrice B LEFT JOIN Product A ON A.ID=B.ProductId
where not exists(SELECT 1 FROM ProductPrice WHERE ProductId=B.ProductId AND price<B.price)
------解决思路----------------------
SELECT a.id,a.NAME,b.id AS productPriceID,b.xh,b.price
FROM product a
inner JOIN (
SELECT id,productid,xh,price FROM ProductPrice a
WHERE NOT EXISTS(SELECT 1 FROM ProductPrice WHERE productId=a.productid
AND price<a.price)
) b ON a.id=b.productid