-2

我有三个数据库表

    Graduate_survey
    id(pk)AI  PO1  PO2  PO3 

    Alumni_survey
    id(pk)AI  PO1  PO2  PO3 

    faculty_survey
    id(pk)AI  PO1  PO2  PO3

在将值插入这些表并通过三个表的联合计算平均值之后,现在我必须将平均值存储在另一个表中

total_survey id(pk)AI PO PO1 PO2 我的代码是

               <?php 
                @include("connection.php");
               if($_POST['submit'])
                 {
                 $sql= "SELECT t.id
               , AVG(t.PO1) AS total_PO1, AVG(t.PO2) AS total_PO2, 
                 AVG(t.PO3) AS total_PO3
                 FROM ( SELECT Graduate_survey.id
                , Graduate_survey.PO1,  Graduate_survey.PO2,  
                 Graduate_survey.PO3
                FROM Graduate_survey
                 UNION 
                 SELECT alumni_survey.id
                 , alumni_survey.PO1, alumni_survey.PO2, alumni_survey.PO3
                FROM alumni_survey
                 UNION
                 SELECT faculty_survey.id
                , faculty_survey.PO1, faculty_survey.PO2, faculty_survey.PO3
                 FROM faculty_survey
                 )t
                GROUP BY t.id 
                ORDER BY t.id";
               $rData=mysql_query($sql);
               $res=mysql_fetch_array($rData);
               $sql="insert into total_survey 
              values('','total_PO1','total_PO2','total_PO3')";
               mysql_query($sql);

               }
               ?>
              <form method="post">
              <p align="center"><input type="submit" name="submit" 
              value="Click here to calculate the final indirect assesment"> 
              </p>
               </form>        

选择查询工作正常,但插入查询不工作。问题是我必须在另一个表 total_survey 中插入选择查询显示的数据。如何将获取的数据存储在 MySQL 的另一个表中?

4

1 回答 1

1

You will need to replace some of my generic naming, but below is what you're after

INSERT INTO `SchemaName`.total_survey (`ColumnName1`, `ColumnName2`, `ColumnName3`, `ColumnName4`)

SELECT t.id, AVG(t.PO1) AS total_PO1, AVG(t.PO2) AS total_PO2, AVG(t.PO3) AS total_PO3
FROM ( SELECT Graduate_survey.id, Graduate_survey.PO1, Graduate_survey.PO2, Graduate_survey.PO3
        FROM Graduate_survey

        UNION 

        SELECT alumni_survey.id, alumni_survey.PO1, alumni_survey.PO2, alumni_survey.PO3
        FROM alumni_survey

        UNION

        SELECT faculty_survey.id, faculty_survey.PO1, faculty_survey.PO2, faculty_survey.PO3
        FROM faculty_survey
 )t
GROUP BY t.id 
ORDER BY t.id
于 2017-05-23T21:47:00.937 回答