vendredi 12 février 2016

SQL: How can I add to a table without some duplicates?

So I am currently trying to take one table and add it into another table but for some reason it is not working the way I want it to. There are three columns in both the tables and I only want to add each row of data from table 2 to table 1 if the first 2 columns of table 2 are not already in table 1 (I dont care about the 3rd column)

This is what I have so far:

INSERT INTO table1 (col1, col2, col3)
    SELECT a.col1, a.col2, a.col3 from table2 as a
    WHERE NOT EXISTS (SELECT b.col1, b.col2 from table1 as b
    WHERE a.col1 = b.col1 AND a.col2 = b.col2);

I checked around and this seems that it should work but it isn't but can anyone see why?

Aucun commentaire:

Enregistrer un commentaire