Home >Database >Mysql Tutorial >How to Find the Latest Date for Each Group and All Models with the Latest Date in MySQL?
To obtain the latest date of each model group, please use the following inquiries:
This query first group the row in the
<code class="language-sql">SELECT model, MAX(date) AS latest_date FROM doc GROUP BY model;</code>
line, and then find the maximum date in each group. The result is a table containing two columns: model
(identifiable grouping) and doc
(indicating the latest date of the group). model
latest_date
If you need to find all models with the latest date:
This query finds the same date as the latest date in the entire table.
<code class="language-sql">SELECT model, date FROM doc WHERE date = (SELECT MAX(date) FROM doc);</code>
Other options
Scenes that need to obtain detailed information from each record that matches the latest date, you can use the following inquiries:
MySQL 8.0 and higher versions provides
clauses, which can provide faster results for larger data sets:<code class="language-sql">SELECT d.model, d.date, d.color, d.etc FROM doc d WHERE d.date = (SELECT MAX(d2.date) FROM doc d2 WHERE d2.model = d.model);</code>
The above is the detailed content of How to Find the Latest Date for Each Group and All Models with the Latest Date in MySQL?. For more information, please follow other related articles on the PHP Chinese website!