How to insert Distinct Records from Table A to Table B (both tables have same structure)

前端 未结 4 861
天涯浪人
天涯浪人 2021-01-05 15:25

I want to insert only Distinct Records from Table \"A\" to Table \"B\". Assume both the tables has same structure.

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-05 15:57

    If by DISTINCT you mean unique records that are on TableB that aren't already in TableA, then do the following:

    INSERT INTO TableB(Col1, Col2, Col3, ... , Coln)
    SELECT DISTINCT A.Col1, A.Col2, A.Col3, ... , A.Coln
    FROM TableA A
    LEFT JOIN TableB B
    ON A.KeyOfTableA = B.KeyOfTableB
    WHERE B.KeyOfTableB IS NULL
    

提交回复
热议问题