php - 如何将分数作为另一表中值的平均值插入到一个表中?

标签 php mysql

这是我从表单中插入分数的第一个查询:

$query1 = "INSERT INTO Category(timing, musicality, technique, difficulty, performance_id)
      VALUES ('{$timing}', '{$musicality}', '{$technique}', '{$difficulty}','{$performance}')"
      ;

这有效,它将分数放入单独的分数表中,现在我尝试取这些分数的平均值并将它们放入参赛者信息所在的表中,我将一列留空以供分数进入这是我尝试过的查询:

$query2 = "Insert into Contestants (score) select 
sum(timing + musicality + technique + difficulty)/(select count(timing)* 4 from Category where performance_id = '{$performance}')
From Category
WHERE performance_id = '{$performance}';";

发生的情况是,它将正确的平均分数插入到我想要的表中的正确字段中,但它不是根据 Performance_id 更新行,而是创建一个新行。我该如何解决这个问题?

最佳答案

试试这个:

$query2 = "UPDATE Contestants SET score = (select 
sum(timing + musicality + technique + difficulty)/(select count(timing)* 4 from Category where performance_id = '{$performance}')
From Category
WHERE performance_id = '{$performance}') 
WHERE performance_id = '{$performance}';";

关于php - 如何将分数作为另一表中值的平均值插入到一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20465286/

相关文章:

javascript - div与包含复选框的数组的动态对齐

php - 如何使用 PHP value.PHP Mysql 插入表

mysql - 从编程角度识别和非识别关系

php - 什么相当于在条令中编写任务的存储库?

PHP 查询正确但结果不匹配?

php - php 表单中的提交按钮也可以作为 anchor 吗?

php - Mysql插入准备好的语句不起作用

mysql - 当我使用内联接进行更新时,更改 ON 列子句会影响更新吗?

mysql - 通过连接 7 个表创建 SQL View

PHPExcel - 克隆工作表并保持其原始样式