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

时间:2013-12-09 07:25:18

标签: 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更新行,而是创建一个新行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

$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}';";