我的表格列名为displayorders,值为1到250,并且不是自动增量。
现在我想在displayorder
= 3的表中添加一个新行。所以我不想手动更新从3到250的所有值。相反,我想要更新所有的displayorders到+1和我可以手动从1更改为2(即更新后2到3)。如何通过SQL查询执行此操作?
答案 0 :(得分:19)
如果我理解正确,您需要运行UPDATE
语句,如下所示:
UPDATE your_table SET displayorder = displayorder + 1 WHERE displayorder > 2;
测试用例:
CREATE TABLE your_table (displayorder int);
INSERT INTO your_table VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9);
UPDATE
声明后的结果:
SELECT * FROM your_table;
+--------------+
| displayorder |
+--------------+
| 1 |
| 2 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
| 10 |
+--------------+
9 rows in set (0.00 sec)
答案 1 :(得分:3)
更新yourTableName set displayorder = displayorder + 1其中displayorder> 2
答案 2 :(得分:3)
UPDATE MyTable SET displayorders=displayorders+1 WHERE displayorders>2