Quantcast
Channel: Select row with most recent date per user - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Select row with most recent date per user

$
0
0

I have a table ("lms_attendance") of users' check-in and out times that looks like this:

id  user    time    io (enum)1   9   1370931202  out2   9   1370931664  out3   6   1370932128  out4   12  1370932128  out5   12  1370933037  in

I'm trying to create a view of this table that would output only the most recent record per user id, while giving me the "in" or "out" value, so something like:

id  user    time    io2   9   1370931664  out3   6   1370932128  out5   12  1370933037  in

I'm pretty close so far, but I realized that views won't accept subquerys, which is making it a lot harder. The closest query I got was :

select     `lms_attendance`.`id` AS `id`,    `lms_attendance`.`user` AS `user`,    max(`lms_attendance`.`time`) AS `time`,    `lms_attendance`.`io` AS `io` from `lms_attendance` group by     `lms_attendance`.`user`,     `lms_attendance`.`io`

But what I get is :

id  user    time    io3   6   1370932128  out1   9   1370931664  out5   12  1370933037  in4   12  1370932128  out

Which is close, but not perfect. I know that last group by shouldn't be there, but without it, it returns the most recent time, but not with it's relative IO value.

Any ideas?Thanks!


Viewing all articles
Browse latest Browse all 15

Latest Images

Trending Articles



Latest Images