Copy rows from one table to another, ignoring duplicates

前端 未结 9 1329
半阙折子戏
半阙折子戏 2020-11-27 13:44

I have 2 tables (srcTable1 & destTable) that have identical schemas. I am trying to copy all rows from srcTable to destTable and ignore the duplicates. I thought I could

相关标签:
9条回答
  • 2020-11-27 14:42

    I realize this is old, but I got here from google and after reviewing the accepted answer I did my own statement and it worked for me hope someone will find it useful:

        INSERT IGNORE INTO destTable SELECT id, field2,field3... FROM origTable
    

    Edit: This works on MySQL I did not test on MSSQL

    0 讨论(0)
  • 2020-11-27 14:44

    Your problem is that you need another where clause in the subquery that identifies what makes a duplicate:

    INSERT INTO destTable
    SELECT Field1,Field2,Field3,... 
    FROM srcTable
    WHERE NOT EXISTS(SELECT * 
                     FROM destTable 
                     WHERE (srcTable.Field1=destTable.Field1 and
                           SrcTable.Field2=DestTable.Field2...etc.)
                     )
    

    As noted by another answerer, an outer join is probably a more concise approach. My above example was just an attempt to explain using your current query to be more understandible. Either approach could technically work.

    INSERT INTO destTable
    SELECT s.field1,s.field2,s.field3,... 
    FROM srcTable s 
           LEFT JOIN destTable d ON (d.Key1 = s.Key1 AND d.Key2 = s.Key2 AND...)
    WHERE d.Key1 IS NULL
    

    Both of the above approaches assume you are woried about inserting rows from source that might already be in destination. If you are instead concerned about the possibility that source has duplicate rows you should try something like.

    INSERT INTO destTable
    SELECT Distinct field1,field2,field3,... 
    FROM srcTable  
    

    One more thing. I'd also suggest listing the specific fields on your insert statement instead of using SELECT *.

    0 讨论(0)
  • 2020-11-27 14:45
    insert into tbl2
    select field1,field2,... from tbl1
    where not exists 
        ( 
            select field1,field2,... 
            from person2
            where (tbl1.field1=tbl2.field1 and
            tbl1.field2=tbl2.field2 and .....)
        )
    
    0 讨论(0)
提交回复
热议问题