Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PGSQL: Selecting values from first table and inserting them as one of the params to the second table

Is is possible to do something like this or similar in PostgreSQL?

INSERT INTO table_2 (some_varchar_value, id_value_table_1)
VALUES ('some_varchar_value', SELECT DISTINCT id FROM table_1)

In short I want to select ids from table_1 and insert them as a second param of insert to table_2. The first of inserted values will be a const of varchar type

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’d use INSERT INTO ... SELECT ...:

INSERT INTO table_2 (some_varchar_value, id_value_table_1)
SELECT DISTINCT 'some_varchar_value', id
FROM table_1;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading