如何根据MySQL中的当前值更新字段?

时间:2021-09-15 17:08:09

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"

例如,table1中ID为“1”的记录的值为“2”

SELECT table1
WHERE ID = "1"
SET RESULT to CurrentID
RESULT = CurrentID + 1;

2 个解决方案

#1


13  

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":

对于ID列等于“1”的任何行,这会将结果列设置为同一行的CurrentID列(加1)的值:

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

#2


4  

You might try

你可以试试

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

#1


13  

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":

对于ID列等于“1”的任何行,这会将结果列设置为同一行的CurrentID列(加1)的值:

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

#2


4  

You might try

你可以试试

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