Postgres INSERT INTO with SELECT

Red*_*irt 6 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)
Run Code Online (Sandbox Code Playgroud)

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

Ild*_*sin 17

它应该是这样的:

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

请参阅postgres INSERT 语法