简体   繁体   English

如何在单个数据库中合并两个表?

[英]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. 我确实有两个表,说table1table2两个表都具有相同的结构,但pk索引值不同。 so repetation is minimum. 因此重复最少。

table1 has got 56000 datas

table2 has got 23000 datas

I want merge/import table2 to table1. 我想将table2合并/导入到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. PK中可能有一些重复,所以我需要忽略该数据(不要导入/重写或复制)并导入其余数据。 I am using phpmyadmin so I want do it through that. 我正在使用phpmyadmin,所以我想通过它做到。

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 这是链接: http : //dev.mysql.com/doc/refman/5.0/en/insert-select.html

Have you tried using UNION DISTINCT into another table? 您是否尝试过将UNION DISTINCT用于另一个表?

http://dev.mysql.com/doc/refman/5.0/en/union.html 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. 抱歉,如果语法不正确-请勿在此处使用MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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