Insert into … values ( SELECT … FROM … )

前端 未结 26 2435
我在风中等你
我在风中等你 2020-11-21 05:40

I am trying to INSERT INTO a table using the input from another table. Although this is entirely feasible for many database engines, I always seem to struggle t

26条回答
  •  攒了一身酷
    2020-11-21 05:57

    Just use parenthesis for SELECT clause into INSERT. For example like this :

    INSERT INTO Table1 (col1, col2, your_desired_value_from_select_clause, col3)
    VALUES (
       'col1_value', 
       'col2_value',
       (SELECT col_Table2 FROM Table2 WHERE IdTable2 = 'your_satisfied_value_for_col_Table2_selected'),
       'col3_value'
    );
    

提交回复
热议问题