-- ALWAYS initialize user variables, they default to NULL otherwise.
SET @prevID:=@runSum:=0;
UPDATE teamrank t -- one row per team?
JOIN members m USING ( teamID ) -- multiple rows per team?
SET rank =
IF( @prevID != m.teamID, /* Capture when a teamIDs changes */
( @runSum := m.rank ) + ((@prevID := m.teamID)*0), /* reset both @runSum and @prevTeam */
( @runSum := @runSum + m.rank) /* increment running sum */
)
-- It is important to have proper sequencing, so to reset @runSum when a teamID changes.
ORDER BY t.teamID
;
I've made an assumptions that the logic needed to perform a team rank, is the sum of the ranks for individuals within a team.
This same technique allows one to perform any kind of running sum or counter that needs to be reset when a 'group' changes.
-- J Jorgenson --