如何通过在单个查询中添加MySQL数据库来更新MySQL数据库表中的字段

问题描述:

我有一个表,该表存储将随着时间的推移添加到其中的值.当我想添加值时,我想在一个查询中而不是-

I have a table that stores a value that will be added to over time. When I want to add to the value I would like to do so in a single query rather than -

  1. 从数据库获取oldValue
  2. newValue = oldValue + X
  3. 使用newValue更新行

  1. Get oldValue from database
  2. newValue = oldValue + X
  3. update row with newValue

$ query1 =从表WHERE id = thisID中选择值"; $ result1 = mysql_query($ query1); while($ row = mysql_fetch_array($ result)){ $ oldValue = $ row ['value']; } $ newValue = $ oldValue + x $ query1 =更新表SET值= $ newValue WHERE id = thisID";

$query1 = "SELECT value FROM table WHERE id = thisID"; $result1 = mysql_query($query1); while($row=mysql_fetch_array($result)) { $oldValue = $row['value']; } $newValue = $oldValue + x $query1 = "UPDATE table SET value = $newValue WHERE id = thisID";

这可以在单个查询中完成吗?

Can this be done in a single query?

UPDATE table SET value = value + x WHERE id = thisID