Page 1 of 1

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

Posted: Fri Dec 12, 2008 4:29 am
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.

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

Posted: Fri Dec 12, 2008 6:12 am
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

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

Posted: Fri Dec 12, 2008 7:36 am
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'

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

Posted: Sat Dec 13, 2008 4:02 am
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'