Insert multiple rows WITHOUT repeating the “INSERT INTO …” part of the statement?

前端 未结 15 1853
广开言路
广开言路 2020-11-22 09:16

I know I\'ve done this before years ago, but I can\'t remember the syntax, and I can\'t find it anywhere due to pulling up tons of help docs and articles about \"bulk import

15条回答
  •  有刺的猬
    2020-11-22 09:42

    This will achieve what you're asking about:

    INSERT INTO table1 (ID, Name)
        VALUES (123, 'Timmy'), 
               (124, 'Jonny'), 
               (125, 'Sally');
    

    For future developers, you can also insert from another table:

    INSERT INTO table1 (ID, Name)
        SELECT 
             ID, 
             Name 
        FROM table2
    

    Or even from multiple tables:

    INSERT INTO table1 (column2, column3)
        SELECT 
             t2.column, 
             t3.column
        FROM table2 t2
             INNER JOIN table3 t3
             ON t2.ID = t3.ID
    

提交回复
热议问题