开发者

How can I update a field in a MySQL database table by addition in MySQL database in a single query

开发者 https://www.devze.com 2022-12-28 20:43 出处:网络
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 -

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. Get oldValue from database
  2. newValue = 开发者_运维问答oldValue + X
  3. update row with newValue

    $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


UPDATE table SET field = oldValue + X WHERE id = 1
0

精彩评论

暂无评论...
验证码 换一张
取 消