简体   繁体   English

Mysql将数据从一个表插入到另一个表

[英]Mysql Insert data from one table to another table

INSERT INTO table2 (firstname, lastname, middlename) SELECT firstname, lastname FROM table1 WHERE id = 1

How can I insert data for table2 'middlename' column? 如何为table2“中间名”列插入数据? In my table1 there is only two column 'firstname' and 'lastname' 在我的表1中,只有两列“名字”和“姓氏”

Remove the middlename column from the list: 从列表中删除middlename列:

INSERT INTO table2 (firstname,lastname) 
SELECT firstname, lastname FROM table1 WHERE id = 1

试试这个查询

INSERT INTO table2 (firstname, lastname, middlename) SELECT firstname, lastname, "NA" FROM table1 WHERE id = 1
INSERT INTO table2 (firstname,lastname) SELECT firstname, lastname FROM table1

将table2中的中间名留空,因为您没有记录

If it's a mandatory column (ie NOT NULL and no default defined), you'll have to select a constant. 如果这是必填列(即NOT NULL且未定义默认值),则必须选择一个常量。 Something like 就像是

insert into table2 (firstname, lastname, middlename)
select firstname, lastname, 'N/A' from table1 where id = 1;

If the column is optional, omit the column from the column list. 如果列是可选的,请从列列表中省略该列。 Like 喜欢

insert into table2 (firstname, lastname)
select firstname, lastname from table1 where id = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM