如何合并两个表?

发布于 2024-10-11 10:49:24 字数 256 浏览 1 评论 0原文

我确实有两个表,每个表都有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月蹉跎了容颜 2024-10-18 10:49:24

使用 PHP 或其他喜欢的语言,这将是大约 10-20 行代码......

Use PHP or another favorite language, this will be about 10-20 lines of code...

猫烠⑼条掵仅有一顆心 2024-10-18 10:49:24

如果table_1和table_2首先需要有不同的键才能成功合并,

table_1键必须从1-50000开始,table_2键值必须从50001-100000开始

对于重复,您可以使用更新功能来处理在mysql中

INSERT INTO table_1
    SELECT * FROM table_2
ON DUPLICATE KEY UPDATE table_1.val=table_1.val

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

INSERT INTO table_1
    SELECT * FROM table_2
ON DUPLICATE KEY UPDATE table_1.val=table_1.val
情深如许 2024-10-18 10:49:24

如果存在主键列,如果那里有重复项,则必须将其从插入中排除。

一种解决方案是使用删除重复值的选择进行插入。

INSERT IGNORE INTO table_1 (f1, f2, f3)
SELECT f1, f2, f2 FROM table_2 WHERE table_2.index NOT IN (SELECT index FROM table_1)

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.

INSERT IGNORE INTO table_1 (f1, f2, f3)
SELECT f1, f2, f2 FROM table_2 WHERE table_2.index NOT IN (SELECT index FROM table_1)

OBS, this is not very efficient on a slow database server and could be written better with more information on the tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文