Anand mohan sinha
Anand mohan sinha

Reputation: 588

How to increment a database entry?

The main problem is suppose i have a value stored in database = 26 now if the user does some action and i have to increse the value by 1 is there any predefined way in sql to do it.

Right now i am extracting the value then adding 1 then again updating the entry.

Upvotes: 0

Views: 201

Answers (1)

Fluffeh
Fluffeh

Reputation: 33502

Yes, run an SQL like below:

update yourTableName 
    set theColumnYouWant=theColumnYouWant+1 
    where yourConditions=YourConditionCriteria

A concrete example of the update syntax:

mysql> select * from first;
+------+-------+
| bob  | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    5 | eeee  |
+------+-------+
5 rows in set (0.00 sec)

mysql> update first set bob=bob+1 where title='eeee';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from first;
+------+-------+
| bob  | title |
+------+-------+
|    1 | aaaa  |
|    2 | bbbb  |
|    3 | cccc  |
|    4 | NULL  |
|    6 | eeee  |
+------+-------+
5 rows in set (0.00 sec)

Upvotes: 6

Related Questions