Copying data from one table to another table. Databases are different and table structure is different

匆匆过客 提交于 2019-12-04 14:38:07
insert into db2.table2 (field1,field2,..,fieldN)
select field1,field2,..,fieldN from db1.table1

EDIT. If you need to do an update between two different databases this is the right syntax:

update 
db2.table2 as t2,
db1.table1 as t1
set 
t2.field1 = t1.field1,
t2.field2 = t1.field2,
t2.field3 = t1.field3
where t1.id = t2.id

If both databases are on the same server then the easiest way is to use INSERT INTO... SELECT query

INSERT INTO
   database2.table2 (c1, c2, c3)
SELECT
   c2, c4, MD5(c3)  --you can choose only these columns that are needed as well as use functions to convert data to required format if needed
FROM
   database1.table1

This will copy all the data from one table to another (MySQL)

INSERT INTO  `databse_name`.`tablename_copy` 
SELECT * 
FROM  `databse_name`.`tablename` ;

if you set primary key this will create some problem. Please check the primary key duplication error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!