Insert into … values ( SELECT … FROM … )

前端 未结 26 2499
我在风中等你
我在风中等你 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条回答
  •  -上瘾入骨i
    2020-11-21 06:05

    Claude Houle's answer: should work fine, and you can also have multiple columns and other data as well:

    INSERT INTO table1 ( column1, column2, someInt, someVarChar )
    SELECT  table2.column1, table2.column2, 8, 'some string etc.'
    FROM    table2
    WHERE   table2.ID = 7;
    

    I've only used this syntax with Access, SQL 2000/2005/Express, MySQL, and PostgreSQL, so those should be covered. It should also work with SQLite3.

提交回复
热议问题