更新 table 并按 ID 移动

UPDATE table with shift by ID

我希望用新列更新table,同时引用另一列中的前一行。

ID | Fruit  | Fruit_prev |
-------------------------
1  | Apple  |            |
2  | Orange | Apple      |
3  | Banana | Orange     |
4  | Lemon  | Banana     |

原始 table 中不存在 Fruit_prev,我正在尝试使用 UPDATE 列添加它。

我的想法是这样的:

UPDATE table SET Fruit_prev = (SELECT Fruit WHERE ID = ID-1);

然而,这在 MYSQL 中不起作用。

完成此任务的最佳方法是什么?

更新默认 ID:

UPDATE FruitTable AS ft
INNER JOIN FruitTable AS ft2
    ON ft.ID - 1 = ft2.ID
SET ft.Fruit_prev = ft2.Fruit

UPDATE +1 移位任何列(这里是日期列):

UPDATE
Fruit as t0
INNER JOIN
  (SELECT ft.*, 
         @rownum := @rownum + 1 AS rank
    FROM Fruit ft, 
         (SELECT @rownum := 0) r
  ORDER BY Date) AS t1
INNER JOIN
  (SELECT ft.*, 
         @rownum2 := @rownum2 + 1 AS rank
    FROM Fruit ft, 
         (SELECT @rownum2 := 0) r
  ORDER BY Date) AS t2
ON t0.Id = t1.Id AND t1.rank - 1= t2.rank
SET t0.FruitPrev = t2.Fruit;

这里连接了三个表。第一个是默认的,所以它可以更新,第二个和第三个加上 Date 的排名,所以它们可以被那个 rank 字段移动 +1。