Simplified, but for a table like:
id time distance price 1 20 500 8 2 50 500 10 3 90 500 12 4 80 1000 17 5 170 1000 11 6 180 1000 13 7 19 800 12
I want to get the rows with the quickest time for the distances 500 and 1000, i.e.
id time distance price 1 20 500 8 4 80 1000 17
If I do
select min(time) from table
that works fine for finding the price, but I can’t get the id and price – only the max/min/average/first value of all ids/prices.
I can do it with multiple look ups – e.g.
select * from table where distance = 500 and time = 20 select * from table where distance = 1000 and time = 80
but is there a better way that doesn’t involve 1 + (number of distances) queries (or at least provides one resultset, even if internally it uses that number of queries)
You will need to use an inner select: