是否可以在查询表上使用INSERT函数插入行?

时间:2017-03-25 09:02:24

标签: mysql

INSERT INTO (
    SELECT student_info.Student_Name,scores.Final
    FROM student_info 
    INNER JOIN scores ON scores.Student_id=student_info.Student_id 
        AND scores.Subject_id=1)
(Student_Name,Final) VALUES("a",1)

像这样......我想要实现的是我想在查询结果中添加一个新行,该行将显示其上方列的平均值。

2 个答案:

答案 0 :(得分:1)

使用SELECT student_info.Student_Name,scores.Final FROM student_info INNER JOIN scores ON scores.Student_id=student_info.Student_id AND scores.Subject_id=1 UNION SELECT "a", AVG(scores.Final) FROM student_info INNER JOIN scores ON scores.Student_id=student_info.Student_id AND scores.Subject_id=1 将两个查询的结果合并为一个结果:

oSession.oRequest["..."] = "...";

答案 1 :(得分:0)

如果您需要插入选择,您可以使用这种方式

INSERT INTO your_table_name  (Student_Name,Final)
SELECT student_info.Student_Name,scores.Final
FROM student_info 
INNER JOIN scores ON scores.Student_id=student_info.Student_id 
AND scores.Subject_id=1

并且最终如果需要添加一行,您可以将一个带有文字值的union子句添加到您的选择

INSERT INTO your_table_name   (Student_Name,Final)
SELECT student_info.Student_Name,scores.Final
FROM student_info 
INNER JOIN scores ON scores.Student_id=student_info.Student_id 
AND scores.Subject_id=1
union all  
select "a", 1
from dual;
相关问题