如何根据 MySQL 中的当前值更新字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5668195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update a field based on its current value in MySQL?
提问by blunders
Is it possible to get the current value of a field, use it as a variable in a calculation, then update the field based on the result?
是否可以获取字段的当前值,将其用作计算中的变量,然后根据结果更新该字段?
For example the record with the ID "1" in table1 has a value of "2"
例如,表 1 中 ID 为“1”的记录的值为“2”
SELECT table1
WHERE ID = "1"
SET RESULT to CurrentID
RESULT = CurrentID + 1;
回答by Oded
This will set the resultcolumn to the value of the CurrentIDcolumn (plus 1) of the same row, for any row that has an IDcolumn that equals "1":
对于具有等于“1”的列的任何行,这会将result列设置为CurrentID同一行的列的值(加 1)ID:
UPDATE table1
SET result = CurrentID + 1
WHERE ID = "1"
回答by John K.
You might try
你可以试试
Update table1 set ID = ID+1 where ID = 1

