Answer the question
In order to leave comments, you need to log in
How to select recent records by a specific field?
Good afternoon.
It is necessary to select from the mySQL database of the `keys` table only the latest data for the `serial` field:
## таблица keys
serial | event_date_time | rest_data..
------ | --------------- | -----------
sn005 | 2015-05-05 | data1
sn001 | 2015-01-01 | data1
sn001 | 2016-06-06 | data2
sn001 | 2020-10-10 | data3
sn005 | 2019-09-09 | data2
serial | event_date_time | rest_data..
------ | --------------- | -----------
sn001 | 2020-10-10 | data3
sn005 | 2019-09-09 | data2
SELECT t.* FROM
(SELECT * FROM `keys` ORDER BY event_date_time DESC) t
GROUP BY t.serial
Answer the question
In order to leave comments, you need to log in
SELECT `k`.*
FROM (
SELECT `serial`, MAX(`event_date_time`) AS `event_date_time`
FROM `keys`
GROUP BY `serial`
) AS `d`
JOIN `keys` AS `k`
ON `k`.`serial` = `d`.`serial` AND `k`.`event_date_time` = `d`.`event_date_time`
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question