15

Referring to How to insert values into a table from a select query in PostgreSQL?,

I would like to INSERT into a table rows from another, specified by a SELECT DISTINCT, plus some static values, something like:

INSERT INTO new_tbl (column1, column2, column3)
SELECT DISTINCT id FROM -- long where clause --, 
 'a string', 0;

So that every row in the new table will get the same values for column2 and column3 Is this possible?

asked Mar 14, 2016 at 9:17

1 Answer 1

19

You can put static values into SELECT clause.

INSERT INTO new_tbl (column1, column2, column3)
 SELECT DISTINCT id, 'a string', 0 FROM -- long where clause --;
answered Mar 14, 2016 at 9:34
2
  • 2
    facepalm... how on earth was I not seeing this? select query too long ;) Commented Mar 14, 2016 at 9:55
  • 1
    Happens to the best of us ;) Commented Feb 21, 2021 at 8:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.