Home > Database > Mysql Tutorial > How to Find the Latest Date for Each Group and All Models with the Latest Date in MySQL?

How to Find the Latest Date for Each Group and All Models with the Latest Date in MySQL?

DDD
Release: 2025-01-24 08:47:12
Original
965 people have browsed it

How to Find the Latest Date for Each Group and All Models with the Latest Date in MySQL?

Get the latest date in the data of mysql grouping

To obtain the latest date of each model group, please use the following inquiries:

This query first group the row in the
SELECT model, MAX(date) AS latest_date
FROM doc
GROUP BY model;
Copy after login

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.
SELECT model, date
FROM doc
WHERE date = (SELECT MAX(date) FROM doc);
Copy after login

<选> 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:
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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template