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

时间:2014-01-28 12:17:37

标签: php mysql

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

如何为table2'middlename'列插入数据?在我的table1中,只有两列'firstname'和'lastname'

4 个答案:

答案 0 :(得分:0)

从列表中删除middlename列:

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

答案 1 :(得分:0)

尝试此查询

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

答案 2 :(得分:0)

INSERT INTO table2 (firstname,lastname) SELECT firstname, lastname FROM table1

将table2中的中间名留空,因为你没有

的记录

答案 3 :(得分:0)

如果它是必填列(即NOT NULL且未定义默认值),则必须选择常量。 像

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

如果列是可选的,请忽略列列表中的列。像

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