简体   繁体   中英

How do I merge two tables in single database?

I do have two table say table1 and table2 both tables have same structure but pk index values are different. so repetation is minimum.

table1 has got 56000 datas

table2 has got 23000 datas

I want merge/import table2 to table1. there may be some repetations in PK so I need to ignore that data(dont want to import/rewrite or duplicate) and import rest of the data. I am using phpmyadmin so I want do it through that.

I'm pretty sure you can do:

INSERT IGNORE INTO table1 SELECT * FROM table2;

to ignore duplicate keys.

However, please try it on a third table first ;)

Here's a link: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

Have you tried using UNION DISTINCT into another table?

http://dev.mysql.com/doc/refman/5.0/en/union.html

So you could write something like this:

insert into table3
select * from table1 
union distinct 
select * from table2

Hope that helps.

Sorry if syntax is not spot on - don't use MySQL here in work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM