0

假设我们有表格:

announces_id    announces_pos   announces_title

如何通过单个查询重新计算所有announces_pos?示例:将 id 2 上的announces_pos 设置为 3,应重新计算所有下一行(announces_pos > 3)

4

1 回答 1

1

你需要老位置。比将新旧位置之间的所有位置加 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;
于 2013-02-01T06:33:06.577 回答