Perform INSERT with SELECT to insert multiple records

后端 未结 7 1296
情深已故
情深已故 2021-02-05 08:13

In the diagram below there is a 1:1 relationship between \'DodgyOldTable\' and \'MainTable\'. Table \'Option\' contains records with \'OptionVal1\', \'OptionVal2\' and \'OptionV

7条回答
  •  余生分开走
    2021-02-05 08:40

    11 Yrs later but might help someone:

    INSERT INTO schema.tableName (col1, col2,col3, col4, col5, col6, col7)
          SELECT col1, 
                 col2,
                 'Static value',
                 CURRENT_TIMESTAMP,
                 'Anothe static value', 
                 1, 
                 (SELECT col7 FROM schema2.anotherTableName)
           FROM schema.tableX;
    

    col1 & col2 come from the same select

    col3, col5 are static strings

    col4 is the current system time

    col6 a Num value,

    col7 from a select that returns one value.

    NB: Ensure the select with multiple values comes at the beginning. This is MySQL

提交回复
热议问题