本文介绍了如何通过在单个查询中添加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

这篇关于如何通过在单个查询中添加MySQL数据库来更新MySQL数据库表中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:58