Emiel Steerneman
Emiel Steerneman

Reputation: 402

Postgresql: INSERT INTO using SELECT and values

Postgresql version: 9.1.9

I'm trying to insert data into a table using SELECT and passing values

Example of what i try to accomplish:

current database:

MyTable
character    number
'a'            '0'
'b'            '1'
'c'            '1'

what i want:

MyTable
character    number
'a'            '0'
'b'            '1'
'c'            '1'
'b'            '2'
'c'            '2'

I tried different things but i can't seem to get it right. For example:

INSERT INTO MyTable (character, number)
    (SELECT character FROM MyTable WHERE number = '1', '2')

Note: The numbers are actually long strings and therefore have to be in brackets.

Thanks.

Upvotes: 13

Views: 19970

Answers (1)

pobrelkey
pobrelkey

Reputation: 5973

INSERT INTO MyTable (character, number) 
    SELECT character, '2' FROM MyTable WHERE number = '1'

to save the data in the same format on your example

Upvotes: 23

Related Questions