Update a Table Field to its Value Plus a Constant

2019-07-07 06:36发布

I would like to perform an UPDATE in MYSQL in which I take a field value, add a constant and save the new value in the same field.

Let's assume that we have a column called OldValue in a table called aTable.

Pseudocode could be:

UPDATE aTable SET OldValue = OldValue + 220 WHERE someField = someValue

Do you have any idea on how I could do that? I would like to use a single query (on some administration panel) without making a php script. (Of course, in that case the answer is quite simple.)

2条回答
Rolldiameter
2楼-- · 2019-07-07 07:10

Here is a working example using Pseudocode from original question itself.

UPDATE `table1` SET `field1` = `field1` - 18, `field2` = `field2` + 16 WHERE n_id IN (111,222,333) OR form IN ('Digital Editing','Studio Room') AND location_id LIKE ('%home-page10%')
查看更多
看我几分像从前
3楼-- · 2019-07-07 07:16

Your code will work.

See it working in SQLFiddle.

I used your exact query verbatim, except for substituting a real value for "someValue".

查看更多
登录 后发表回答