简体   繁体   中英

How can I insert the score into one table as an average of values from another table?

This my first query to insert the scores from the form:

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

This works and it places the scores into a individual scores table, now im trying to take the average of these scores and put them into a table where the contestants information is located, I left a column blank for the scores to go into and heres the query I tried for that:

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

Whats happening is, it is inserting the correct average score into the table that I want it to, in the correct field, but instead of updating the row based on the performance_id, it creates a new row. How can I fix this?

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM