且构网

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

从另一个具有不同列数的表插入(MySql)

更新时间:2023-12-01 09:35:34

运行时:

insert into one
    select * from two;

SQL引擎自动将其包含在隐含的列中.

The SQL engine automatically puts in the columns that are implied.

  • 对于insert,这是声明顺序的列列表.
  • 对于*,这是声明顺序的列列表.
  • For the insert, this is the list of columns in declaration order.
  • For the *, this is the list of columns in declaration order.

按名称没有列的匹配",只有每个表中的列的列表.

There is no "matching" of columns by names, only lists of columns in each table.

因此,查询实际上是:

insert into one(a, b)
    select b from two;

在我看来这是一个错误.

That looks like an error to me.

故事的道德?编写您想要的代码.始终包括列列表,尤其是对于insert语句.所以写:

Moral of the story? Write that code that you intend. Always include columns lists, particularly for insert statements. So write:

insert into one(b)
    select b from two;