How can I SQL insert a record for every id in another table?

This question already has an answer here:

  • Insert into … values ( SELECT … FROM … ) 21 answers

  • The correct syntax does not use values :

    insert into table1(col1, . . . )
        select distinct id, 'blah', 'blah', 'blah'
        from table2;
    

    Notes:

  • You should always use explicit column lists when using insert , unless you really, really know what you are doing.
  • Use single quotes to delimit a string, not double quotes.

  • The syntax looks like this:

    insert into table1
    select distinct id, 'blah', 'blah', 'blah' from table2
    

    Use single quotes for string literals, like 'blah' . (Double quotes are for delimited identifiers, eg if an object has an reserved word as name "table" .)

    链接地址: http://www.djcxy.com/p/94314.html

    上一篇: 将数据从一个表复制到另一个表并更改ID

    下一篇: 我怎样才能SQL插入另一个表中的每个ID的记录?