且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将一个表的所有值插入到另一个表中的 SQL

更新时间:2022-12-09 16:24:11

insert 语句实际上有这样做的语法.如果您指定列名而不是选择*"会容易得多:

The insert statement actually has a syntax for doing just that. It's a lot easier if you specify the column names rather than selecting "*" though:

INSERT INTO new_table (Foo, Bar, Fizz, Buzz)
SELECT Foo, Bar, Fizz, Buzz
FROM initial_table
-- optionally WHERE ...

我***澄清一下,因为出于某种原因,这篇文章得到了一些反对票.

I'd better clarify this because for some reason this post is getting a few down-votes.

INSERT INTO ... SELECT FROM 语法适用于您要插入的表(在我上面的示例中为new_table")已经存在的情况.正如其他人所说,SELECT ... INTO 语法适用于您想在命令中创建新表的情况.

The INSERT INTO ... SELECT FROM syntax is for when the table you're inserting into ("new_table" in my example above) already exists. As others have said, the SELECT ... INTO syntax is for when you want to create the new table as part of the command.

您没有指定是否需要在命令中创建新表,因此如果目标表已存在,则 INSERT INTO ... SELECT FROM 应该没问题.

You didn't specify whether the new table needs to be created as part of the command, so INSERT INTO ... SELECT FROM should be fine if your destination table already exists.