mysql - How to select only last entries based on a especific column value keeping the current table order -
i have table keeps track of last changes applied students in student catalog. want return data table every minute can sincronize necessary entries java app.
it happens need last event_type entry per id. i've created small art graphically explain want site doesnt let me post because of reputation being low yet. i'll try post link though:
what have +---------------------+------------+------------+ +---------------------+------------+------------+ | event_time | event_type | student_id | | event_time | event_type | student_id | +---------------------+------------+------------+ +---------------------+------------+------------+ | 2015-08-16 11:42:08 | 1 | 37 | | 2015-08-16 11:44:30 | 1 | 37 | | 2015-08-16 11:42:29 | 1 | 37 | | 2015-08-16 11:45:47 | 2 | 37 | | 2015-08-16 11:43:51 | 2 | 37 | | 2015-08-16 12:21:40 | 1 | 44 | | 2015-08-16 11:44:13 | 3 | 37 | | 2015-08-16 12:21:49 | 3 | 44 | | 2015-08-16 11:44:30 | 1 | 37 | | 2015-08-16 12:21:55 | 2 | 30 | | 2015-08-16 11:45:47 | 2 | 37 | | 2015-08-16 12:22:00 | 3 | 37 | | 2015-08-16 12:21:28 | 2 | 44 | | 2015-08-16 12:27:08 | 2 | 44 | | 2015-08-16 12:21:40 | 1 | 44 | +---------------------+------------+------------+ | 2015-08-16 12:21:49 | 3 | 44 | | 2015-08-16 12:21:55 | 2 | 30 | | 2015-08-16 12:22:00 | 3 | 37 | | 2015-08-16 12:27:08 | 2 | 44 | +---------------------+------------+------------+
any idea how do that? searched site couldnt find suitable this. help!
e.g.:
select x.* my_table x join my_table y on y.student_id = x.student_id , y.event_time >= x.event_time group x.student_id , x.event_time having count(*) <= 3 order event_time;
or, faster...
select event_time , event_type , student_id ( select x.* , case when @prev = student_id @i := @i + 1 else @i := 1 end , @prev := student_id my_table x , (select @i:=1,@prev:='') vars order student_id , event_time desc ) n <= 3 order event_time;
Comments
Post a Comment