简体   繁体   English

如何将具有不同列数的行从一个表复制到另一表

[英]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 ,具有7idid_commodityparametervalueunitstypenotes

table2: trade_commodity and has 8 column id , id_trade , id_commodity , parameter , value , units , type , notes table2:trade_commodity,具有8idid_tradeid_commodityparametervalueunitstypenotes

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.

相关问题 如何使用SQL Select插入将行从一个表复制到另一个表 - How can i use SQL Select Insert to copy rows from one table to another 将数据从一个 sql 表复制到另一个具有较少列数和不同列的表 - copy data from one sql table to another having less number of columns and a different column 如何从数据库表中获取行数并使用 UPDATE 查询将总行数插入到不同表的列中 - How can I fetch the number of rows from a database table and insert the total rows into columns of a different table using UPDATE query 如何将一行从一个表复制到另一个表 - How can I copy a row from one table to another 如何将行从一个MySQL表复制到另一个表并基于变量更新值之一 - How do I copy rows from one MySQL table to another and update one of the values based on a variable 如何将图像从一个表插入(或复制)到另一表 - How can I insert (or copy) an image from one table to another table MySQL将具有特定列和UNIX时间戳的所有行从一个表复制到另一个表 - Mysql copy all rows from one table to another with specific columns and unix timestamp 如何在一张表的多行中添加多列,以及在另一张表中添加一行? - How can I add multiple columns from multiple rows in 1 table along with 1 row from another? 如何使用联接基于另一个表中的行从一个表中获取mysql行? - How can I use joins to fetch mysql rows from one table based on rows that are found in another? 如何显示一个表中没有的数据行? - How can I show rows from one table that aren't in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM