Home » questions » Way to increment / decrement field value with MySQL / PHP?

Way to increment / decrement field value with MySQL / PHP?

2006-08-09 10:41:43, Category: Programming & Design
I want to increment or decrement the value of a field in a mysql table and i was wondering if there is an easier way than what i thought of... 1. Get value, 2. value++, 3. Update value on Mysql. That seems really inefficient to me (2 query calls just to push a number up by one), so i thought maybe mysql has a function to do this automatically. Thanks for the help.

Answers

  1. NC

    On 2006-08-09 10:46:36


    You can do it with a single UPDATE query: UPDATE the_table SET the_field = the_field + 1 WHERE [your WHERE clause]
  2. John J

    On 2006-08-09 10:47:58


    you can use a query to increment or decrement a field in just one query: "update table set field = field+1 where ..."