Update a Table Field to its Value Plus a Constant on MYSQL w/o PHP

余生颓废 提交于 2019-12-23 11:13:32

问题


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.)


回答1:


Your code will work.

See it working in SQLFiddle.

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




回答2:


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%')


来源:https://stackoverflow.com/questions/14605924/update-a-table-field-to-its-value-plus-a-constant-on-mysql-w-o-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!