mysql - update next row based on previous ID -


let have table named banners. columns are:

+---+-------------------------------+ |id | link  | image       | active  | +---+-------+-------------+---------+ |1  |#link1 | image1      | 0       | |2  |#link2 | image2      | 1       | |3  |#link3 | image3      | 0       | +---+-------+-------------+---------+ 

there can see row #2 active. how can update next row based on latest active row? if active row last row, set first row active row.

ps: query using cron, update every 2 hours example. no problem cron, did it.

use stored procedure :

delimiter // create procedure updateactiverow() begin     select max(id) @activeid banners active;     update banners set active=1 id > @activeid limit 1;     if row_count() = 0         update banners set active=1 order id limit 1;     end if;     update banners set active=0 id = @activeid;/*do if want deactivate current active row*/ end // 

Comments