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开发者_StackOverflow社区 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'
.
精彩评论