MySQL - updating the average from one table into another table -
i'm mysql/php newbie , i'm sure simple question. i'm trying calculate average of several questions , respondents 1 table , updating group table value.
for example table answers consists of (name, group_id, taskclarity1, taskclarity2, taskclarity3) in table b want (group_id, avg(taskclarity1,taskclarity2,taskclarity3)).
this i've got...
$avg_task_clarity_1 = mysql_query("select avg(taskclarity1) gruppid = '$group_id'"); $avg_task_clarity_2 = mysql_query("select avg(taskclarity2) gruppid = '$group_id'"); $avg_task_clarity_3 = mysql_query("select avg(taskclarity3) gruppid = '$group_id'"); $avg_task_clarity = ($avg_task_clarity_1+$avg_task_clarity_2+$avg_task_clarity_3)/3; $print_task_clarity_1" update results set results.taskclarity = '$avg_task_clarity'"; if (mysql_query($print_task_clarity_1)) { echo $print_task_clarity_1; } else { echo "error taskclarity1: " . mysql_error();
first, mysql_query() returns resource, , need extract information it. query doesn't mantion table name (i'll call mytable). also, can 3 averages 1 query.
here's how start:
$table = "mytable"; $sql = "select avg(taskclarity1) avgclarity1, avg(taskclarity2) avgclarity2, avg(taskclarity3) avgclarity1 $table gruppid = '$group_id'"; $resource = mysql_query($sql); //execute query if (! $resource = mysql_query($sql) ){ echo "error reading table $table"; die; } if (! mysql_num_rows($resource ) ){ echo "no records found in $table"; } else { $row = mysql_fetch_assoc($resource); // fetch first row $avg_task_clarity_1 = $row['avgclarity1']; $avg_task_clarity_2 = $row['avgclarity2']; $avg_task_clarity_3 = $row['avgclarity3']; $avg_task_clarity = ($avg_task_clarity_1+$avg_task_clarity_2+$avg_task_clarity_3)/3; //... // other stuff want }
please comment if not helpful enough, , revise answer.
Comments
Post a Comment