Ashley Banks
Ashley Banks

Reputation: 528

Incrementing a field in a database

I've never had or attempted to do this before, but what I want to do is instead of getting the value out of the database and adding 1 to it just to do an update query is just do one UPDATE query and incrementing the field...

Is there a way of using a JOIN query to get the current value and then updating it again with the new one?

As it's literally just incrementing an INT number I thought there may be way around it.

Upvotes: 1

Views: 68

Answers (4)

F21
F21

Reputation: 33431

Use this:

UPDATE yourtable SET yourcolumn = yourcolumn+1 WHERE yourid=123;

Upvotes: 1

Alp
Alp

Reputation: 29749

It's easy to increment a value in a cell:

UPDATE table SET column = column + 1 WHERE id = 123;

Upvotes: 0

Andreas Wong
Andreas Wong

Reputation: 60594

You could just do the following:

UPDATE table_name set field_to_increment = field_to_increment + 1 WHERE <cond>

Upvotes: 1

Sujit Agarwal
Sujit Agarwal

Reputation: 12518

update table_name set field_name=field_name + 1 where <condition>;

Upvotes: 7

Related Questions