6

I am attempting the following query in Postgres 9.6.3

INSERT INTO join_table ("table_1_id", "table_2_id") VALUES
      (7, SELECT "id" from "table_2" where label='Foo Bar');

This throws ERROR: syntax error at or near "SELECT" at character 94

I have seen examples for nested selects inside of an insert statement work where only the things being selected are inserted. Is the above query even possible?

1
  • Why do you need to use double quotes? Commented Nov 19, 2017 at 13:01

2 Answers 2

15

Try putting parens around the sub-query:

INSERT INTO join_table ("table_1_id", "table_2_id") VALUES
      (7, (SELECT "id" from "table_2" where label='Foo Bar'));
Sign up to request clarification or add additional context in comments.

1 Comment

Ahh I knew it would be something simple! Thank you.
5

Use insert . . . select. values is unnecessary:

INSERT INTO join_table (table_1_id, table_2_id)
    SELECT y, "id" 
    FROM "table_2" 
    WHERE label = 'Foo Bar';

This also allows you to insert multiple rows from the other table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.