如何将 +1 添加到已有值

发布于 2024-09-24 20:55:48 字数 201 浏览 3 评论 0原文

我有一个表,其列名称为 displayorders,值为 1 到 250,并且它不是自动增量。

现在我想在表中添加一个新行,其中 displayorder = 3。所以我不想手动将所有值从 3 更新为 250。相反,我想将所有 displayorder 更新为+1,我可以手动从1更改为2(即更新后2更改为3)。我如何通过SQL查询来做到这一点?

I have table with the column name displayorders with values 1 to 250 and it's not autoincrement.

Now I want to add a new row in table with displayorder = 3. So I don't want to manually update all the values form 3 to 250.Instead of that I want to update all the displayorders to +1 and I can manually change from 1 to 2(ie 2 to 3 after updation).How can I do it through SQL Query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏の忆 2024-10-01 20:55:48

如果我理解正确,您需要运行如下的 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)

If I understood correctly, you'd want to run an UPDATE statement like this:

UPDATE your_table SET displayorder = displayorder + 1 WHERE displayorder > 2;

Test case:

CREATE TABLE your_table (displayorder int);

INSERT INTO your_table VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9);

Result after the UPDATE statement:

SELECT * FROM your_table;
+--------------+
| displayorder |
+--------------+
|            1 |
|            2 |
|            4 |
|            5 |
|            6 |
|            7 |
|            8 |
|            9 |
|           10 |
+--------------+
9 rows in set (0.00 sec)
池木 2024-10-01 20:55:48
UPDATE MyTable SET displayorders=displayorders+1 WHERE displayorders>2
UPDATE MyTable SET displayorders=displayorders+1 WHERE displayorders>2
满身野味 2024-10-01 20:55:48

更新 yourTableName set displayorder = displayorder + 1 其中 displayorder > 2

update yourTableName set displayorder = displayorder + 1 where displayorder > 2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文