How to insert distinguishing records from table A to table B (both tables have the same structure)

I want to insert only individual records from table "A" into table "B". Assume that both tables have the same structure.

+3
source share
4 answers
INSERT INTO B SELECT DISTINCT * FROM A

You might not want the table id column to be part of a separate check, so use this solution if this is the case: fooobar.com/questions/1795809 / ...

+8
source
INSERT INTO TableB
    (Col1, Col2, ...)
    SELECT DISTINCT Col1, Col2, ...
        FROM TableA
+3
source

DISTINCT , TableB, A, :

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
+3
    INSERT INTO TableB
            SELECT *
            FROM TableA AS A
            WHERE NOT EXISTS(SELECT * FROM TableB AS B WHERE B.Field1 = A.Field1) 
-- If need: B.Field2 = A.Field2 and B.Field3 = A.Field3
+1
source

Source: https://habr.com/ru/post/1795809/


All Articles