Seemingly simple MySQL question, but I've never had to do this before..
I have two tables, items and prices, with a one-to-many relationship.
Items Table
id, name
Prices Table
id, item_id, price
Where
prices.item_id = items.id
What I have so far:
SELECT items.id, items.name, MIN(prices.price)
FROM items
LEFT JOIN prices ON items.id = prices.item_id
GROUP BY items.id
How do I also return the corresponding prices.id for that minimum price? Thanks!
New, working answer, based on the final example in the MySQL 5.0 Reference Manual - 3.6.4. The Rows Holding the Group-wise Maximum of a Certain Column:
The
LEFT JOIN
works on the basis that whenprices.price
is at its minimum value, there is nofilter.price
with a smaller value and thefilter
rows values will be NULL.Original incorrect answer:
Ok, how about?
This will return multiple records for a record in Items if there are multiple Prices records for it with the minimum price: