How to update a field in a MySQL database table by adding one query to a MySQL database

I have a table that stores a value that will be added over time. When I want to add a value that I would like to make in a single query, and not -

  • Get oldValue from database
  • newValue = oldValue + X
  • update row using 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 one request?

+3
source share
2 answers
UPDATE table SET value = value + x WHERE id = thisID
+10
UPDATE table SET field = oldValue + X WHERE id = 1
0

Source: https://habr.com/ru/post/1742981/


All Articles