简体   繁体   English

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

[英]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.

相关问题 选择一行中具有最大值的行,另一列中具有最小值的行 - Select the row with max value in one column value and min value in another 在mysql中,如何仅选择一列中具有相同值但在另一列中具有不同值的每一行? - In mysql, how do I select ONLY every row that has the same value in one column but different ones in another? MySQL选择总和,最大值,和最大值保持不变的同一行中的另一列值 - mysql select sum, max, and another column value at the same row which the max value stays MySQL:选择按列分组的所有条目,在另一列中使用相同的值 - MySQL: Select all entries grouped by a column with a common value in another column MySQL返回最大值,如果一列没有值,则返回null - MySQL return max value or null if one column has no value SQL Select 行,其中列具有一定的长度和最大值 - SQL Select row where column has certain length and max value mySQL 获取具有列最大值的行 - mySQL Fetch the row which has the Max value for a column 在MySql中获取两列具有最大值的行 - Fetch the row which has the Max value for two column in MySql 在具有分组结果集的MySQL查询的一列中获取具有最大值的行 - Get row with max value in one column for MySQL query with grouped resultset MySQL:选择一个组的Last Max()值,但由另一个组汇总 - MySQL: Select Last Max() Value of One Group But Summarize by Another Group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM