简体   繁体   中英

MySQL select row that has a common value on one column but max value of another

Here is my table in SQL Fiddle . I want to find 1 row for each children_id where the maximum in_time exists. So the expected output is:

----+------------+
id  |  in_time   |
----+------------+
16  | 1518909618 |
18  | 1518913186 |
17  | 1518909862 |
----+------------+

Here is my query but is not giving expected data:

SELECT a.id, a.in_time
FROM `ca_attendance` AS a
LEFT JOIN (
    SELECT MAX(id) AS id, in_time
    FROM ca_attendance
    GROUP BY children_id
) AS b ON a.id = b.id AND a.in_time = b.in_time
SELECT a.id, a.in_time
FROM `ca_attendance` AS a
 JOIN (
    SELECT MAX(in_time) AS in_time, children_id
    FROM ca_attendance
    GROUP BY children_id

) AS b ON a.children_id = b.children_id and a.in_time = b.in_time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM