MySQL: Increment an exisitng value

Contributor Icon Contributed by qmchenry  
Tag Icon Tagged: MySQL  

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.

 

28 Comments -


  1. Android said on December 10, 2008

    Super, just what I needed to know.

  2. TeyBobster said on March 31, 2009

    What would be the syntax for decrementing/incrementing a number and not an image?

  3. 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.

  4. ohmyfile.com said on August 29, 2009

    Thank you

  5. Harpreet Khera said on October 23, 2009

    Thanks this is what i was looking for…..

  6. Anonymous said on December 29, 2009

    thanks, just what i need to know (-:

  7. Infected Pixel said on January 6, 2010

    thanks .. i wanted it for my shopping cart product views

  8. Richard Neil Ilagan said on January 15, 2010

    super. just what I was looking for.

  9. anonymous said on January 30, 2010

    thnx, guy! It works!

  10. Gururaj B AShetty said on February 9, 2010

    Nice one.this is the thing I am looking for..

  11. 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.

  12. Anonymous said on June 19, 2010

    Cool info. Thanks for adding to the discussion.

  13. Samthunderbolt said on July 6, 2010

    That’s exactly what I was looking for….thanks! :)

  14. Anonymous said on July 6, 2010

    Thanks!

  15. Ms_ngobeni said on July 19, 2010

    3 Years later and this is still helping people!!! thanks

  16. Pieter Kegel said on January 6, 2011

    Thanks! You’re the man!

  17. Nick said on February 3, 2011

    Four people can’t understand MySQL. =(

  18. Guest said on February 10, 2011

    If I want to increment all Ids then what should i Do

  19. Pieterkegel said on March 5, 2011

    Remove the WHERE clause…

  20. Tulanling said on March 19, 2011

    wakakka

  21. boonanny said on April 3, 2011

    Nice ! this is the tweak I need.

  22. 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

  23. christian louboutin outlet said on May 9, 2011

    oh my!! i love you so much!!

    Thank you so much.

  24. Mihai Stefan said on June 6, 2011

    http://bit.ly/lVwd56 :)

  25. Shohan said on September 10, 2011

    Thanks, i was searching for this

  26. Akbar Firdiansyah said on October 15, 2011

    This is what I need, thank you very much :D

  27. lyrics-to.srimp3.com said on October 20, 2011

    oh! thank you very much

  28. Jessy said on November 29, 2011

    Very helpful code. Thank you……..

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -