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.










Android said on December 10, 2008
Super, just what I needed to know.
TeyBobster said on March 31, 2009
What would be the syntax for decrementing/incrementing a number and not an image?
Joggy said on April 11, 2009
He ain’t incrementing an image, that is just the name of the table, and pretty sure that ‘counter’ is a tuple in the table, and that it’s an Integer.
ohmyfile.com said on August 29, 2009
Thank you
Harpreet Khera said on October 23, 2009
Thanks this is what i was looking for…..
Anonymous said on December 29, 2009
thanks, just what i need to know (-:
Infected Pixel said on January 6, 2010
thanks .. i wanted it for my shopping cart product views
Richard Neil Ilagan said on January 15, 2010
super. just what I was looking for.
anonymous said on January 30, 2010
thnx, guy! It works!
Gururaj B AShetty said on February 9, 2010
Nice one.this is the thing I am looking for..
Coastal Web said on June 18, 2010
If the table you’re updating has a unique key such as an ID or URL field, you can extend an INSERT statement to include the above UPDATE in one go:
INSERT INTO images (name, counter) VALUES (‘blah.gif’, 1) ON duplicate KEY UPDATE counter = counter + 1
Assuming name needs to be unique.
Anonymous said on June 19, 2010
Cool info. Thanks for adding to the discussion.
Samthunderbolt said on July 6, 2010
That’s exactly what I was looking for….thanks! :)
Anonymous said on July 6, 2010
Thanks!
Ms_ngobeni said on July 19, 2010
3 Years later and this is still helping people!!! thanks
Pieter Kegel said on January 6, 2011
Thanks! You’re the man!
Nick said on February 3, 2011
Four people can’t understand MySQL. =(
Guest said on February 10, 2011
If I want to increment all Ids then what should i Do
Pieterkegel said on March 5, 2011
Remove the WHERE clause…
Tulanling said on March 19, 2011
wakakka
boonanny said on April 3, 2011
Nice ! this is the tweak I need.
CSS said on April 19, 2011
Awesome Thanks :) Just to help others out.. here is what I used to update a price column in an Open Cart store by $2 in phpmyadmin:
UPDATE `DBNAMEHERE`.`product` SET `price` = `price`+2
Replace DBNAMEHERE with your db name
christian louboutin outlet said on May 9, 2011
oh my!! i love you so much!!
Thank you so much.
Mihai Stefan said on June 6, 2011
http://bit.ly/lVwd56 :)
Shohan said on September 10, 2011
Thanks, i was searching for this
Akbar Firdiansyah said on October 15, 2011
This is what I need, thank you very much :D
lyrics-to.srimp3.com said on October 20, 2011
oh! thank you very much
Jessy said on November 29, 2011
Very helpful code. Thank you……..