问题
I'm creating a DB with "Team" table for some NFL teams, and I have assigned them all a ranking (standing in NFL), the attribute is called "Ranking".
I want to create a trigger such that if the ranking is updated, all of the others are updated appropriately.
However, I can't figure out a way to loop through the table. For example, assume team at rank 5 moves up to rank 3, how do I get the rank 3 to become 4, and then 4 to be 5?
If you need more info, feel free to ask, I'll provide it ASAP.
回答1:
If you know the id of the team to update (lets call it 42) and the old and new rank (old: 5, new: 3), then it's not too hard:
UPDATE Team SET rank=rank+1 WHERE rank BETWEEN 3 AND 5;
UPDATE Team SET rank=3 WHERE id=42;
来源:https://stackoverflow.com/questions/5851019/mysql-trigger-updating-ranking