I have the following data in my database:
|NO | model | date |
+---+-------+----------+
|1 | bee |2011-12-01|
|2 | bee |2011-12-05|
|3 | bee |2011-12-12|
|4 | tar |2011-12-13|
I want to get the latest date of each model group:
| model | date |
+-------+----------+
| bee |2011-12-12|
| tar |2011-12-13|
I tried:
SELECT model, date
FROM doc
WHERE date ........????? //what is the next?
GROUP BY model
Are you looking for the max date for each model?
If you’re looking for all models matching the max date of the entire table…
[— Added —]
For those who want to display details from every record matching the latest date within each model group (not summary data, as asked for in the OP):
MySQL 8.0 and newer supports the
OVERclause, producing the same results a bit faster for larger data sets.