如何在MySQL表中使用UPDATE移位列数据?

时间:2014-10-28 17:57:23

标签: mysql

我想使用这样的静态大小的表:

+------+----------+-----------+
|  id  | col1     | col2      |
+------+----------+-----------+
|  1   | a        | x         |
+------+----------+-----------+
|  2   | b        | y         |
+------+----------+-----------+
|  3   | c        | z         |
+------+----------+-----------+

例如,当我更新[3,col1]时,有没有办法向上移动列数据?表应如下所示......

+------+----------+-----------+
|  id  | col1     | col2      |
+------+----------+-----------+
|  1   | b        | x         |
+------+----------+-----------+
|  2   | c        | y         |
+------+----------+-----------+
|  3   | d*       | z         |
+------+----------+-----------+

* [row3,col1]和列数据中的新值已向上移动;提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用update / join

执行此操作
update table t left join
       table tnext
       on t.id = tnext.id - 1
    set t.col1 = (case when tnext.id is null then 'd' else tnext.col1 end);