Php MySQL更新除法计算错误
我有一个mysql表,如下所示:Php MySQL更新除法计算错误,php,mysql,sql,sql-update,Php,Mysql,Sql,Sql Update,我有一个mysql表,如下所示: 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) N
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 ;
id domain_name presentation_name total_score times_played avg_score
1 test.com test 30 3 10.00
id domain_name presentation_name total_score times_played avg_score
1 test.com test 36 4 8.40
我正在用PHP更新数据但在更新时,查询计算“平均分数”时出错。比如说,整行看起来像这样:
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 ;
id domain_name presentation_name total_score times_played avg_score
1 test.com test 30 3 10.00
id domain_name presentation_name total_score times_played avg_score
1 test.com test 36 4 8.40
但当我使用新数据运行此更新查询时:
$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'";
它变成这样:
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 ;
id domain_name presentation_name total_score times_played avg_score
1 test.com test 30 3 10.00
id domain_name presentation_name total_score times_played avg_score
1 test.com test 36 4 8.40
你可以看到“平均分数”是错误的(应该是9.00)。我在phpmyadmin中尝试了相同的查询,但得到了相同的错误计算。真的找不到我做错了什么。看起来更新是以增量方式进行的 也就是说,总分变为36,播放次数变为4,然后计算平均分数(36+6,4+1)=42/5=8.4 尝试更新另一句话中的平均分数
但不知道是什么在这样做。看起来更新是以增量方式进行的 也就是说,总分变为36,播放次数变为4,然后计算平均分数(36+6,4+1)=42/5=8.4 尝试更新另一句话中的平均分数
但是,不知道是什么原因。看起来前两列得到了更新,更新后的值在更新第三列时使用,请注意
8.4=(36+6)/(4+1)
因此,第三列不需要+1
和+score
尽管您确实不应该在数据库中存储重复数据,因为这只会导致类似这样的问题
只要在php或mysql中计算所需的平均值即可。看起来前两列已更新,更新后的值将在更新第三列时使用,请注意
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
这是正确的 见
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
这是正确的 试试看:
更改顺序(首选方法)
或:
您引用的字段已在后续计算中进行了调整,但假设这些字段保持不变。请尝试:
更改顺序(首选方法)
或:
您引用的字段已在后续计算中进行了调整,但假设它们保持不变。更改:
$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'";
至:
$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'";
更改:
$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'";
至:
$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'";
来自:
下面语句中的第二个赋值将col2设置为
当前(更新的)col1值,而不是原始col1值。结果
col1和col2具有相同的值此行为不同于
标准SQL
UPDATE t1 SET col1 = col1 + 1, col2 = col1;
因此,您可以在更新平均值时忽略+分数和+1:
UPDATE `entries` SET
total_score = total_score + $score,
times_played = times_played + 1,
avg_score = total_score / times_played
WHERE id = '1'
从:
下面语句中的第二个赋值将col2设置为
当前(更新的)col1值,而不是原始col1值。结果
col1和col2具有相同的值此行为不同于
标准SQL
UPDATE t1 SET col1 = col1 + 1, col2 = col1;
因此,您可以在更新平均值时忽略+分数和+1:
UPDATE `entries` SET
total_score = total_score + $score,
times_played = times_played + 1,
avg_score = total_score / times_played
WHERE id = '1'
尝试将
avg_分数
字段转换为十进制(4,2)
字段。在进行此类计算时,浮动可能很棘手。没有浮动不是问题。请尝试将avg_分数
字段转换为十进制(4,2)
字段。在进行这样的计算时,浮动可能会很棘手。没有浮动不是问题。