sql insert into table from select without duplicates (need more then a DISTINCT)

前端 未结 5 580
渐次进展
渐次进展 2021-01-31 11:01

I am selecting multiple rows and inserting them into another table. I want to make sure that it doesn\'t already exists in the table I am inserting multiple rows into.

5条回答
  •  自闭症患者
    2021-01-31 11:44

    So you're looking to retrieve all unique rows from source table which do not already exist in target table?

    SELECT DISTINCT(*) FROM source
    WHERE primaryKey NOT IN (SELECT primaryKey FROM target)
    

    That's assuming you have a primary key which you can base the uniqueness on... otherwise, you'll have to check each column for uniqueness.

提交回复
热议问题