mysql行顺序重新计算

mysql行顺序重新计算,mysql,Mysql,假设我们有表: announces_id announces_pos announces_title 如何通过一次查询重新计算所有公告位置? 例子: 将id 2上的announces_pos设置为3,接下来的所有行(announces_pos>3)都应重新计算您需要返回旧位置。然后将新旧位置之间的所有位置增加1 DO @old_pos:=announces_pos FROM announces WHERE announces_id = 2; UPDATE announces SET

假设我们有表:

announces_id    announces_pos   announces_title
如何通过一次查询重新计算所有公告位置? 例子:
将id 2上的announces_pos设置为3,接下来的所有行(announces_pos>3)都应重新计算

您需要返回旧位置。然后将新旧位置之间的所有位置增加1

DO @old_pos:=announces_pos FROM announces WHERE announces_id = 2;
UPDATE announces SET announces_pos = IF(announces_id = 2, 3, announces_pos + 1) WHERE announces_pos BETWEEN 3 AND @old_pos;

如何重新计算?或者,您是如何得到id 2的值3的?这个问题不清楚(至少对我来说)。这没关系,假设您的id 2有announces_pos=100,当用户将其更改为50时,我们需要将所有行announces_pos的值增加到50到100之间100@Joeeee我认为你应该使用触发器:你能解释一下它是什么意思吗(宣告id=2,3,宣告id+1)它用1个类似的
宣告id=宣告id+1
,宣告id 3在哪里
宣告id=3