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?
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'));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With