PostgreSQL: insert from another table

后端 未结 4 949
悲&欢浪女
悲&欢浪女 2021-01-30 00:39

I\'m trying to insert data to a table from another table and the tables have only one column in common. The problem is, that the TABLE1 has columns that won\'t accept null value

4条回答
  •  星月不相逢
    2021-01-30 01:08

    Just supply literal values in the SELECT:

    INSERT INTO TABLE1 (id, col_1, col_2, col_3)
    SELECT id, 'data1', 'data2', 'data3'
    FROM TABLE2
    WHERE col_a = 'something';
    

    A select list can contain any value expression:

    But the expressions in the select list do not have to reference any columns in the table expression of the FROM clause; they can be constant arithmetic expressions, for instance.

    And a string literal is certainly a value expression.

提交回复
热议问题