开发者

How to update a field based on its current value in MySQL?

开发者 https://www.devze.com 2023-02-26 05:37 出处:网络
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?

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"

SELECT table1
WHERE ID = "1"
SET RESULT to Curre开发者_高级运维ntID
RESULT = CurrentID + 1;


This will set the result column to the value of the CurrentID column (plus 1) of the same row, for any row that has an ID column that equals "1":

UPDATE table1
SET result = CurrentID + 1
WHERE ID = "1"


You might try

Update table1 set ID = ID+1 where ID = 1
0

精彩评论

暂无评论...
验证码 换一张
取 消