[英]MySQL select row that has a common value on one column but max value of another
Here is my table in SQL Fiddle . 这是我在SQL Fiddle中的表 。 I want to find 1 row for each children_id where the maximum in_time exists. 我要为存在最大in_time的每个children_id找到1行。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.