If i had a table that stored a person’s name, the time it took them to run a mile, and when they ran said mile, what would be the best way to get a person most recent lap-time.
LAPS
____________________________________________
| name | lap_time | date |
--------------------------------------------
| George | 20.3 | 2013-01-17 09:17:14 |
| Alex | 32.2 | 2013-02-17 14:24:32 |
| Mike | 16.6 | 2013-01-17 07:57:54 |
| Alex | 28.5 | 2013-01-17 19:50:21 |
| Mike | 15.1 | 2013-02-17 12:37:12 |
| Mike | 14.8 | 2013-03-17 06:58:34 |
''''''''''''''''''''''''''''''''''''''''''''
I’ve been doing it this way, and it has worked for me so far, but I’m curious to know if there is a better way.
SELECT l.lap_time
FROM laps l
INNER JOIN(
SELECT *, MAX(date) as most_recent
FROM laps
WHERE name = 'Alex'
)AS temp ON (
l.date = temp.most_recent
AND l.name = temp.name
)
The actual table that i’m using this type of query on is huge, so i’m looking for the most time efficient way of doing it.
This will work for a single name:
Result
See the demo
This should work for all names:
Result
See the demo