根据table2中的行值将记录插入table1

时间:2018-05-08 14:15:24

标签: postgresql

对于表考试中的每一行'exam.examRegulation isnull',我想在table examRegulation中插入一个相应的行,并将列值从考试复制到考试。显然,以下查询过于天真,必须得到批准:

insert into examRegulation (graduation, course, examnumber, examversion) 
values (exam.graduation, exam.course, exam.examnumber, exam.examversion)
where ?? (select graduation, course, examnumber, examversion
from exam
where exam.examRegulation isnull)

有没有办法在postgresql中执行此操作?

1 个答案:

答案 0 :(得分:1)

您可以将其改为INSERT INTO ... SELECT声明:

INSERT INTO examRegulation (graduation, course, examnumber, examversion)
SELECT graduation, course, examnumber, examversion
FROM exam
WHERE examRegulation IS NULL;

顾名思义,VALUES子句只能与文字值一起使用。如果需要使用查询逻辑填充插入,则需要使用SELECT子句。