这是我从表单中插入分数的第一个查询:
$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/