[英]How can I copy rows from one to another table with a different number of columns
I have a "unusual" problem on MySQL Syntax. 我在MySQL语法上有一个“异常”问题。
I have 2 tables: 我有2张桌子:
table1: commodityAttributes and has 7 column
id
,id_commodity
,parameter
,value
,units
,type
,notes
table1:商品
id_commodity
,具有7列id
,id_commodity
,parameter
,value
,units
,type
,notes
table2: trade_commodity and has 8 column
id
,id_trade
,id_commodity
,parameter
,value
,units
,type
,notes
table2:trade_commodity,具有8列
id
,id_trade
,id_commodity
,parameter
,value
,units
,type
,notes
note: primary key for both tables are 'id', which is I didn't want to copy 注意:两个表的主键都是'id',这是我不想复制的
What I want, is copy all column from table1 to table2 but also create value for id_trade
in table2. 我想要的是将所有列从表1复制到表2,而且还要在表2中为
id_trade
创建值。
Please take a look that table1 and table2 has different number of columns and id_trade
on table2 IS NOT auto_increment. 请查看table1和table2的列数不同,并且table2上的
id_trade
不是auto_increment。
Here is the example of the actual result and desired result: 这是实际结果和期望结果的示例:
table1:
id,id_commodity,parameter,value,units,type,notes
1, 1, 'Ash','10','%','min','ash for a commodity'
2, 1, 'Ash 2','15','%','max','ash for a commodity'
after do copy procedure, it produce: 完成复制过程后,将产生:
table2:
id,id_trade,id_commodity,parameter,value,units,type,notes
1,NULL,1, 'Ash','10','%','min','ash for a commodity'
2,NULL,1, 'Ash 2','15','%','max','ash for a commodity'
what I want is the result of table2: 我想要的是table2的结果:
id,id_trade,id_commodity,parameter,value,units,type,notes
1,10,1, 'Ash','10','%','min','ash for a commodity'
2,10,1, 'Ash 2','15','%','max','ash for a commodity'
which is '10'
for id_trade comes from php var. id_trade的值为
'10'
来自php var。
How can I accomplish this? 我该怎么做? Or is there another tricky?
还是还有另一个棘手的问题? Btw, I am using PHP and MySQL to work on this task.
顺便说一句,我正在使用PHP和MySQL来完成此任务。
EDIT: I see the "similiar" problem with this, but I found that he is trying to use command rather than value MYSQL: How to copy an entire row from one table to another in mysql with the second table having one extra column? 编辑:我看到与此“类似”的问题,但我发现他正在尝试使用命令而不是值MYSQL:如何在mysql中将整个行从一个表复制到另一个表中,而第二个表又有一个额外的列?
Please kind help me, thank you. 请帮助我,谢谢。
Does this work: 这项工作:
insert into table2 (id_commodity,parameter,value,units,type,notes)
select id_commodity,parameter,value,units,type,notes
from table1
UPDATE: In light of additional information provided by the OP, this should be the solution that the OP is looking for: 更新:鉴于OP提供的其他信息,这应该是OP寻找的解决方案:
insert into table2 (id_trade, id_commodity,parameter,value,units,type,notes)
select '10', id_commodity,parameter,value,units,type,notes
from table1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.