MySQL UPDATE: add +1 to row, not auto_increment?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

MySQL UPDATE: add +1 to row, not auto_increment?

Post by JAB Creations »

Is there a way to simply add '1' to an existing integer value in a single MySQL UPDATE query? If so it'd be easier and more efficiency then doing a SELECT query and then an UPDATE query however I haven't been able to find anything and MySQL SUM simply adds all the rows from a certain column up.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: MySQL UPDATE: add +1 to row, not auto_increment?

Post by novice4eva »

i think "update table set number=number+1 where some condition " would work perfectly!! Must work, i recall that i did it in oracle so it must work in mysql too

EDIT: NEVER TRIED IT THOU IN MYSQL, WILL GET TO HOME AND SEE WHAT HAPPENS
EDIT: YEHH IT WORKS
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: MySQL UPDATE: add +1 to row, not auto_increment?

Post by JAB Creations »

Woot! Thanks! Here is what I tested out successfully!

Code: Select all

UPDATE forum_threads SET forum_thread_count_views=forum_thread_count_views+1 WHERE forum_thread_id='1'
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: MySQL UPDATE: add +1 to row, not auto_increment?

Post by JAB Creations »

Here is an example of updating multiple rows in a single query for those who may find the archived version of this thread useful...

Code: Select all

UPDATE forum_forums SET forum_forums_count_posts=forum_forums_count_posts+1, forum_forums_count_threads=forum_forums_count_threads+1 WHERE forum_forums_id='1'
Post Reply