Postgres INSERT INTO with SELECT

时间:2016-03-16 19:40:02

标签: postgresql

I'm trying to write some SQL to insert records into a PG table.

This is the logic:

  • For every record in the costprojects table that has coststatus_id=1
  • insert a new record into costestimates table
    • costcat_id=30, amount=0, costproject_id=costproject.id(from the costprojects record) , maintenance=‘FALSE’, position=22

This is the SQL code I tried:

INSERT INTO costestimates (costcat_id, amount, costproject_id, maintenance, position) VALUES (30, 0, costproject.id, false, 22)
(SELECT id FROM costprojects WHERE coststatus_id=1)

I get ERROR: syntax error at or near "("

2 个答案:

答案 0 :(得分:3)

它应该是这样的:

INSERT INTO costestimates (costcat_id, amount, costproject_id, maintenance, position)
SELECT 30, 0, id, false, 22 FROM costprojects WHERE coststatus_id=1;

请参阅postgres INSERT syntax

答案 1 :(得分:0)

你的语法有点偏;你想要的是使用你的SELECT语句实际组成VALUES列表。

尝试这样的事情:

rm $( find node_modules -name .babelrc)