MySQL 更新除法计算错误

MySQL update calculation wrong for division

我有一个 mysql table 这样的:

CREATE TABLE IF NOT EXISTS `entries` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `domain_name` varchar(255) NOT NULL,
  `presentation_name` varchar(255) NOT NULL,
  `total_score` mediumint(9) NOT NULL,
  `times_played` mediumint(9) NOT NULL,
  `avg_score` float(4,2) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=206 ;

我正在用 PHP
更新数据 但是更新时 'avg_score' 的查询计算错误。比方说,整行看起来像这样:

id   domain_name   presentation_name  total_score  times_played   avg_score
1    test.com      test               30           3              10.00

但是当我 运行 使用新数据进行此更新查询时:

$score = 6;
$query = "UPDATE `entries` 
                        SET 
                            total_score = (total_score + $score), 
                            times_played = (times_played + 1), 
                            avg_score = ( (total_score + $score) / (times_played + 1) ) 
                    WHERE id = '1'";

变成这样:

id   domain_name   presentation_name  total_score  times_played   avg_score
1    test.com      test               36           4              8.40

可以看到'avg_score'是错误的(应该是9.00)。我在 phpmyadmin 中尝试了相同的查询并得到了相同的错误计算。在这里真的找不到我做错了什么。

看起来更新正在逐步完成。

也就是总分变成36,出场次数变成4,然后avg_score的计算就完成了(36 + 6, 4 + 1) = 42/5 = 8.4

尝试用另一个句子更新 avg_score。

虽然不知道这是做什么的。

您的前两列似乎已更新,更新后的值用于更新第三列,请注意 8.4 = (36 + 6) / (4 + 1)

所以您的第三列不需要 +1+ $score

尽管您真的不应该在数据库中存储重复数据,因为那样只会导致这样的问题。

只需要在需要时计算平均值即可,在 php 或 mysql 中。

total_score = (total_score + $score), 

36 = 30 + 6

times_played = (times_played + 1),

4 = 3 + 1

那你就做

avg_score = ( (total_score + $score) / (times_played + 1) )

8.4 = (36 + 6) / 5

正确!

尝试: 更改顺序(首选方法)

                    SET 
                        avg_score = ( (total_score + $score) / (times_played + 1) ) ,
                        total_score = (total_score + $score), 
                        times_played = (times_played + 1)

或:

                    SET 
                        total_score = (total_score + $score), 
                        times_played = (times_played + 1), 
                        avg_score = ( (total_score) / (times_played) ) 

您引用的字段已在后续计算中进行了调整,但假设它们保持不变。

变化:

$score = 6;
$query = "UPDATE `entries` 
                        SET 
                            total_score = (total_score + $score), 
                            times_played = (times_played + 1), 
                            avg_score = ( (total_score + $score) / (times_played + 1) ) 
                    WHERE id = '1'";

收件人:

  $score = 6;
    $query = "UPDATE `entries` 
                            SET 
                                total_score = (total_score + $score), 
                                times_played = (times_played + 1), 
                                avg_score = (total_score /times_played ) 
                        WHERE id = '1'";

SQL Fiddle

来自MySQL manual

The second assignment in the following statement sets col2 to the current (updated) col1 value, not the original col1 value. The result is that col1 and col2 have the same value. This behavior differs from standard SQL.

UPDATE t1 SET col1 = col1 + 1, col2 = col1;

所以更新平均值时可以省略+score和+1:

UPDATE `entries` SET 
    total_score  = total_score  + $score, 
    times_played = times_played + 1, 
    avg_score    = total_score / times_played
WHERE id = '1'