How can I check for duplicates before inserting into a table when inserting by select

后端 未结 4 1913
时光说笑
时光说笑 2021-02-02 02:28

How can I check for duplicates before inserting into a table when inserting by select:

insert into table1
select col1, col2 
from table2

I need

相关标签:
4条回答
  • 2021-02-02 02:55
    insert into table1
    select col1, col2 
    from table2 
    where table2.col1 not in (select col1 from table1)
    
    0 讨论(0)
  • 2021-02-02 03:01

    You can simply add IGNORE into your insert statement.

    e.g

    INSERT IGNORE INTO table1
    SELECT col1, col2 
    FROM table2
    

    This is discussed here

    0 讨论(0)
  • 2021-02-02 03:14
    INSERT INTO table1 
    SELECT t2.col1, 
           t2.col2 
    FROM   table2 t2 
           LEFT JOIN table1 t1 
             ON t2.col1 = t1.col1 
                AND t2.col2 = t1.col2 
    WHERE  t1.col1 IS NULL 
    

    Alternative using except

    INSERT INTO @table2 
    SELECT col1, 
           col2 
    FROM   table1 
    EXCEPT 
    SELECT t1.col1, 
           t1.col2 
    FROM   table1 t1 
           INNER JOIN table2 t2 
             ON t1.col1 = t2.col1 
                AND t1.col2 = t2.col2 
    

    Alternative using Not Exists

    INSERT INTO table2 
    SELECT col1,col2 
    FROM table1 t1
    WHERE
    NOT EXISTS( SELECT 1
        FROM table2 t2
        WHERE t1.col1 = t2.col1
              AND t1.col2 = t2.col2)
    
    0 讨论(0)
  • insert into table1
    select distinct col1, col2 
    from table2 
    
    0 讨论(0)
提交回复
热议问题