简体   繁体   中英

MySQL autoincrement on update

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

  • id
  • total number of ratings
  • total values of ratings

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?

try something like this

update mytable set total=total+1;

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' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM