Tuesday, November 19, 2024
HomeDatabaseMySQLMySQL: Increment an exisitng value

MySQL: Increment an exisitng value

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.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!