gpt4 book ai didi

MySQL 选择某一列具有共同值但另一列具有最大值的行

转载 作者:行者123 更新时间:2023-11-29 10:23:14 27 4
gpt4 key购买 nike

这里是my table in SQL Fiddle 。我想为每个children_id 找到1 行,其中最大in_time 存在。所以预期的输出是:

----+------------+
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

关于MySQL 选择某一列具有共同值但另一列具有最大值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48847656/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com