Log in

View Full Version : Increment a Value Stored in the DB



Moshambi
04-07-2009, 08:59 AM
Hello,

I have some fields in my DB that when a certain thing happens I just want to increment the value in it by a number.

Say the field contains this: 3
After the action I want to add 1 to it so that it will now be this: 4

I have set the field to an INT but I am not sure what kind of SQL statement I would need to write.

Would I use UPDATE to change it or is there some kind of command where I can just INCREMENT it or something?

Thanks for any help!

Twey
04-07-2009, 09:51 AM
UPDATE your_table SET your_field = your_field + 1;

Moshambi
04-07-2009, 06:32 PM
This keeps giving me an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Poll Number 1' SET 'three = three + 1'' at line 1

Twey
04-08-2009, 11:21 AM
That's because ‘Poll Number 1’ is not a valid SQL identifier. If you want to use it then you need to enclose it in backticks (`), but really I suggest you just rename it.