【问题标题】:Quickest way to combine two identical structured tables without duplicate entries组合两个相同的结构化表格而不重复条目的最快方法
【发布时间】:2014-05-31 00:50:08
【问题描述】:

我有两个结构/列完全相同的表。我需要根据第二列(标题)将它们组合起来而不重复。第一列是 ID,这些可能有重复但应该忽略。

数据库结构示例

id(主键,自增),标题(唯一),描述,链接

Ex. Table 1
 1 Bob thisisbob bob.com
 2 Tom thisistom tom.com
 3 Chad thisischad chad.com

Ex. Table 2
 1 Chris thisischris chris.com
 2 Chad thisischad chad.com
 3 Dough thisisdough doug.com

What I need in Table 3
 1 Bob thisisbob bob.com
 2 Tom thisistom tom.com
 3 Chad thisischad chad.com
 4 Chris thisischris chris.com
 5 Dough thisisdough doug.com

两个表各有大约 500 万个条目/行。我需要最有效的方式来组合它们。

【问题讨论】:

    标签: mysql database-administration


    【解决方案1】:

    要确切地理解你想要什么有点困难。不过,也许这可能是:

    select *
    from table1
    union all
    select *
    from table2
    where not exists (select 1 from table1 where table1.title = table2.title);
    

    使用table1(title) 上的索引将运行得更快。

    编辑:

    如果你想将它们插入到第三张表中,你可以这样做:

    create table table3 as
        select *
        from table1
        union all
        select *
        from table2
        where not exists (select 1 from table1 where table1.title = table2.title);
    

    【讨论】:

    • @user1155594 。 . .我认为这就是这个答案的作用。
    • 我认为当他说“合并”时,他的意思是将合并的记录插入到新表中 (table 3)。不在查询中选择它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2020-03-30
    • 2021-12-10
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多