我有两个表(ca和average_table)。
ca有三列:Group_id、Group_name、ca_value
average_table有两列:Group_name、ca_value
我想计算ca中所有组的 ca_value 平均值,并用此数据更新 *average_table*。
这就是我所拥有的
//create average table
$average_table="CREATE TABLE average_table(Group_name CHAR(30) Primary Key, ca_value FLOAT)";
// Execute query
if (mysql_query($average_table))
{
echo "Table average created successfully";
}
else
{
echo "Error creating table: " . mysql_error();
}
//Calculate average
$ca = mysql_query("Select AVG(ca_value) FROM ca");
$data_sql = "INSERT INTO average_table (Group_name, ca_value) Values ('Average all groups',$ca)";
// Execute query
if (mysql_query($data_sql))
{
}
else
{
echo "Error Selecting group: " . mysql_error();
}