MySQL: Increment an exisitng value

Home -> Database -> MySQL

10601 views

From the computer of: qmchenry (339 recipes)
Created: Feb 24, 2007     Updated: Mar 05, 2007


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

This slick MySQL syntax allows you to increment or decrement an existing number in a table without first having to read the value. This is a nice way to increment an access counter.

To increment the value 'counter' by one for the row in table 'images' where 'image_id' is '15', use:

UPDATE images SET counter=counter+1 WHERE image_id=15


To decrement the value, use 'counter=counter-1' instead. Incrementing or decrementing by other values (or using whatever valid arithmetic arm flexing you need) will work, too.

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Display a list of databases on a MySQL server
  Solve: Can't connect to local MySQL server through socket /tmp/mysql.sock
  Connect to a MySQL server using the mysql command
  Determine the version of MySQL server
  Describe the column structure of a MySQL table
  Create a MySQL database
  Select a MySQL database to use
  Drop or delete a table in MySQL
  Create a MySQL user account
  Import CSV file directly into MySQL

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.