GreenSaber
GreenSaber

Reputation: 1148

PostgreSQL mass insert into a table?

I need to mass insert all the values from col_a into another table. I can do it one at a time like this:

INSERT INTO table_2 (col_a_id)
SELECT 'col_a_id'
FROM table_1
WHERE col_a = 'x';

But is there a way I can just insert all the columns?

EDIT

Lets say I have this table:

Col_a    |    Col_b    |
------------------------
1        |      a      |
2        |      b      |
3        |      c      |

Instead of checking what is in col_a can I just insert each instance of col_a into a table? so I'll have 1, 2 & 3 in table_2?

Upvotes: 1

Views: 184

Answers (1)

Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48177

INSERT INTO table_2 (col1, col2, col3, .... , coln)
SELECT col1, col2, col3, .... , coln
FROM table_1
WHERE col_a = 'x';

Note: String are separated by single quote

SELECT 'this is a string'

Fieldname use double quote:

SELECT "myFieldName", "col1"

EDIT:

If you want check all columns for 'x'

WHERE 'x' IN (col1, col2, col3, .... , coln)

Upvotes: 1

Related Questions