MySQL- Trigger updating ranking

孤街浪徒 提交于 2019-12-11 12:37:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!