select * from order
-------------------
|orderID|productID|
-------------------
| 1 | 234 |
| 2 | 234 |
| 3 | 123 |
-------------------
select * from product_supplier
-------------------------------------------
|ID|supplierID|productID|cost_price|latest|
-------------------------------------------
|1 | 1 | 234 | 1.00 | 0 |
|2 | 1 | 234 | 0.50 | 1 |
|3 | 2 | 123 | 0.75 | 1 |
-------------------------------------------
desired result
------------------------------
|orderID|productID|cost_price|
------------------------------
| 1 | 234 | 1.00 |
| 2 | 234 | 1.00 |
| 3 | 123 | 0.75 |
------------------------------
I'm looking join the two tables above to get the orderID
, productID
and the largest cost_price
for a given productID
.
SELECT orderID, productID, cost_price
FROM order LEFT JOIN product_supplier
ON order.productID=product_supplier.productID AND MAX(cost_price);
gives ERROR 1111 (HY000): Invalid use of group function
How can i restrict the join
ed table to one row per order
, joined with the largest corresponding cost_price
value in product_supplier
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…