Page 1 of 1

Solved: Increment a number in database

Posted: Fri Mar 14, 2014 1:51 pm
by mister_v
Hi,

I want to update a number in a database each time with 1.

Now I first do a Select to get the old number.
and than update with the new number.

Code: Select all

SELECT num FROM database WHERE id='x';
$newnum=$oldnum+1;
UDPATE database set num='$oldnum' WHERE id='x';
Is there a faster way?

Re: Increment a number in database

Posted: Fri Mar 14, 2014 6:56 pm
by chris
You can try the following, it should work in MySQL:

Code: Select all

UDPATE table SET num= num +1 WHERE id='x';
This way MySQL is doing the counting, and not your application or script.
Doesn't have to +1, it can do any counting.

Re: Increment a number in database

Posted: Mon Mar 17, 2014 7:17 am
by mister_v
Thanks that worked :-)