Yamiko
Yamiko

Reputation: 5453

MySQL autoincrement on update

I'm trying to make a table for reviews on blogs. In my table, I want to have these columns:

That way I can just get the average rating with simple math.

Is there a way that I autoincrement 1 to the column that has the total number of ratings, and add the rating to the total number of total ratings without having to retrieve the information first?

For example, in PHP terms:

//instead of doing 
$column=$currentValue;
$column=$column+5;

//do
$column+=5;

is this possible with a MySQL update function if the columns are INT?

Upvotes: 0

Views: 1117

Answers (2)

Johan
Johan

Reputation: 76597

You can use this statement:

UPDATE table1 SET total = IFNULL(total,0) + 1;

This will set total to 1 if it was null before and increase it otherwise.
This is a bit of a hack though, better to create column total as default '0'.

Upvotes: 2

bumperbox
bumperbox

Reputation: 10214

try something like this

update mytable set total=total+1;

Upvotes: 6

Related Questions