php - MYSQL: SELECT * FROM `user_log` WHERE `id` is sequential and `username` == EQUAL in multiple rows -
i've searched high , long answer this. have database collects data whenever user logs onto our network. users complaining of disconnections, crawl database, , find sections user appearing in database on 3 sequential rows.
database structure is:
id user 1 mike 2 john 3 mike 4 mike 5 mike 6 john 7 john 8 mike
i query return below (mike user logged on 3 sequential id's)
id user 3 mike 4 mike 5 mike
i'm stumped how attack this.
i'm thinking like:
select * `user_log` `id` sequential??? , `username` == ???
possibly sub-select ?
what need establish grouping identifier each consecutive sequence of users, , use temporary table perform query groups on new grouping identifier. that, grab group has 3 or more rows, , can use min/max
values of id
show range. need use variables accomplish this.
select min(id), max(id), user ( select if(@prev != user, if(@prev := user, @rc := @rc +1, @rc := @rc + 1), @rc) g, id, user log, (select @prev := -1, @rc := 0) q order id desc ) q group g having count(g) >= 3;
this part: (select @prev := -1, @rc := 0) q
initialises variables can in single statement.
Comments
Post a Comment