I am trying to number some rows on a bridge table with a single UPDATE/SELECT statement using a counter variable @row. For example:
UPDATE teamrank JOIN (SELECT @row := @row + 1 AS position, name FROM members)
USING(teamID, memberID) SET rank = position
Is something like this possible or do I need to create a cursor? If it helps, I am using MySQL 5.
— ALWAYS initialize user variables, they default to NULL otherwise.
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 —