如何合并两个表?
我确实有两个表,每个表都有 50000 条数据。两个表 id 都从 1 - 50000 开始,因此当我尝试使用
INSERT IGNORE
INTO table_1
SELECT *
FROM table_2
它从 table_2 插入到 table_1 时,会完全重写现有的表。我不想重写它,而是添加为新的,所以它总共有 100k。但可能会说索引列中有 5 到 10 个重复项,因此也要对索引进行重复检查。我该怎么做?
I do have two table each with 50000 data. both tables id starts from 1 - 50000 so when I try to insert to table_1 from table_2 by using
INSERT IGNORE
INTO table_1
SELECT *
FROM table_2
it completely rewrites the existing one. I dont want to rewrite it but add as new so it will totallying 100k. but may say 5 to 10 duplicates in index column so duplicate check on index too. how can I do this??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 PHP 或其他喜欢的语言,这将是大约 10-20 行代码......
Use PHP or another favorite language, this will be about 10-20 lines of code...
如果table_1和table_2首先需要有不同的键才能成功合并,
table_1键必须从1-50000开始,table_2键值必须从50001-100000开始
对于重复,您可以使用更新功能来处理在mysql中
If table_1 and table_2 need to have different keys first of all for a successfull merge to occur
table_1 keys will have to start from 1-50000 and table_2 key value will have to start from 50001-100000
For duplication you can deal using the on update functionality in mysql
如果存在主键列,如果那里有重复项,则必须将其从插入中排除。
一种解决方案是使用删除重复值的选择进行插入。
OBS,这在缓慢的数据库服务器上效率不是很高,并且可以通过有关表的更多信息来写得更好。
If there is an primary key column you have to exclude it from the insert if there is duplicates there.
One solution is to insert with a select that removes duplicate values.
OBS, this is not very efficient on a slow database server and could be written better with more information on the tables.