从另一个表中将数据插入到mysql表中

时间:2013-07-09 15:07:00

标签: php mysql

我想将table1中的数据插入到table2中,并将我自己的数据插入到table2中的一个(相同的)查询中,但它无法正常工作。 这是我的查询

$query='insert into table2(id,name) values("001",select first_name from table1 where table1.id="001")';

请有人告诉我我的查询出错了,我会非常高兴。谢谢。

3 个答案:

答案 0 :(得分:2)

insert into table2 (id, name) 
select '001', first_name 
from table1 
where id = '001'

或者您可以使用id,因为它是001

insert into table2 (id, name) 
select id, first_name 
from table1 
where id = '001'

答案 1 :(得分:1)

insert into table2(id,name) 
select "001",first_name from table1 where table1.id="001"

答案 2 :(得分:1)

insert into table2 (id, name)
    select id, first_name from table1 where table1.id = '001';

^为什么硬编码select '001'

相关问题