繁体   English   中英

MySQL选择行,其中一列具有公共值,而另一列具有最大值

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

这是我在SQL Fiddle中的表 我要为存在最大in_time的每个children_id找到1行。 因此,预期输出为:

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

这是我的查询,但未提供预期的数据:

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.

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