1

This is my current data in database.

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

I want to get the maxdate and maxtime for each IC.

I tried:

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

But it only return the last row data for me. The result I wanted is like

    ========================================
    id | IC      |date         |time       |
    2  | test    |2017-07-27   |14:20:26   |
    3  | second  |2017-07-28   |06:58:55   |
    ========================================
Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
ron
  • 175
  • 3
  • 14

4 Answers4

2

This will return the max date & time per ic:

select ic, max(date), max(time)
from check_in
group by ic
Carra
  • 17,808
  • 7
  • 62
  • 75
0

This should work

 select max(time),date,id 
 from (select max(date) as date,time,id from check-in group by time,id ) 
 group by date,id;
pritesh agrawal
  • 1,155
  • 8
  • 16
0

You can use a tuple and group by 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) 
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

Did you try with "OR" condition insteads "AND"?

Luong.Khuc
  • 26
  • 3