The intuitive way to do this is:
UPDATE followers f
SET growth_rate=Total_fans/(select Total_fans from followers f2 where f2.id = f.id-1);
But that, alas, won't work in MySQL because of some complicated prohibition against including the table being updated in a subquery in an update
statement. Instead, use the join
syntax:
update followers f left outer join
followers f2
on f2.id = f.id - 1
set f.growth_rate = f.Total_Fans / f2.Total_Fans;
Then, keep your fingers crossed that Total_fans
is never 0
. Or, check for this explicitly:
update followers f left outer join
followers f2
on f2.id = f.id - 1
set f.growth_rate = (case when f2.Total_Fans > 0 then f.Total_Fans / f2.Total_Fans end);