Solved: Increment a number in database

Post Reply
mister_v
Posts: 180
Joined: Thu Mar 04, 2010 9:19 pm

Solved: Increment a number in database

Post 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?
Last edited by mister_v on Mon Mar 17, 2014 7:18 am, edited 1 time in total.
chris
Site Admin
Posts: 182
Joined: Mon Jul 21, 2008 9:52 am

Re: Increment a number in database

Post 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.
mister_v
Posts: 180
Joined: Thu Mar 04, 2010 9:19 pm

Re: Increment a number in database

Post by mister_v »

Thanks that worked :-)
Post Reply