从查询中选择最大日期和最长时间

时间:2017-07-27 07:02:54

标签: mysql sql

这是我在数据库中的当前数据。

    ========================================
    id | IC      |date         |time       |
    1  | test    |2017-07-27   |14:19:26   |
    2  | test    |2017-07-27   |14:20:26   |
    3  | second  |2017-07-28   |06:58:55   |
    ========================================

我想获得每个IC的maxdate和maxtime。

我试过了:

SELECT id,pass_no,time_in,ic,date_in FROM `check_in` 
WHERE date_in = (SELECT MAX(date_in) FROM check_in) 
AND 
time_in = (SELECT MAX(time_in) FROM check_in) GROUP BY IC

但它只为我返回最后一行数据。我想要的结果就像

    ========================================
    id | IC      |date         |time       |
    2  | test    |2017-07-27   |14:20:26   |
    3  | second  |2017-07-28   |06:58:55   |
    ========================================

4 个答案:

答案 0 :(得分:2)

这将返回最大日期&每个时间:

select ic, max(date), max(time)
from check_in
group by ic

答案 1 :(得分:0)

这应该有效

 select max(time),date,id 
 from (select max(date) as date,time,id from check-in group by time,id ) 
 group by date,id;

答案 2 :(得分:0)

你可以使用元组和ic分组

  SELECT id,pass_no,time_in,ic,date_in 
  FROM `check_in` 
  WHERE (date_in, time_in, ic) in  (
        SELECT MAX(date_in), max(time_id), ic 
          FROM check_in
          GROUP BY id) 

答案 3 :(得分:0)

你试过" OR"条件反而" AND"?