Search code examples
mysqlleft-joinbulkinsertright-join

Trying to insert values from one table to another where a surrogate key match is not found


I am trying to simulate a full outer join, comparing two tables against each other. The batch_import table contains records that need to be inserted, only if they don't exist in the employees table. To do this I have tried the following query:

INSERT INTO employees (forename, surname, employersId, custom_corpore_headOffice, contractId)
SELECT firstname, surname, employeenumber, dob, store, contractId
FROM batch_import
LEFT JOIN employees ON batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId
UNION ALL
SELECT forename, surname, employersId, custom_corpore_headOffice, contractId
FROM batch_import RIGHT JOIN employees ON batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId
WHERE batch_import.employeenumber IS NULL AND batch_import.contractId IS NULL
                       ";

however when I run this query I get the message back from mysql: ERROR 1137 (HY000): Can't reopen table: 'batch_import'

I am guessing it is the logic of my UNION ALL being incorrect. Could someone please help me find a solution?

EDIT:: I have tried this query and it keeps adding records onto the table, starting at the last record. I have also tried:

                    INSERT INTO employees (forename, surname, employersId, dateOfBirth, custom_corpore_headOffice, contractId)
                SELECT firstname, batch_import.surname, employeenumber, dob, store, batch_import.contractId
                FROM batch_import
                LEFT JOIN employees ON batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId
                UNION
                SELECT firstname, batch_import.surname, employeenumber, dob, store, batch_import.contractId
                FROM batch_import RIGHT JOIN employees ON batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId;

but still to no avail. Instead of ignoring what exists it simply writes everything to the end of the table.

Also tried: INSERT INTO employees (forename, surname, employersId, dateOfBirth, custom_corpore_headOffice, contractId) SELECT firstname, batch_import.surname, employeenumber, dob, store, batch_import.contractId FROM batch_import LEFT JOIN employees ON employees.employersId = NULL;


Solution

  • OPTION 1

    INSERT INTO employees (forename, surname, employersId, custom_corpore_headOffice, contractId)
    SELECT firstname, surname, employeenumber, dob, store, contractId
    FROM batch_import
    WHERE NOT EXISTS (Select 1 From employees where batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId)
    

    OPTION 2

    INSERT INTO employees (forename, surname, employersId, custom_corpore_headOffice, contractId)
    SELECT firstname, surname, employeenumber, dob, store, contractId
    FROM batch_import
    LEFT JOIN employees ON batch_import.employeenumber = employees.employersId AND batch_import.contractId = employees.contractId
    WHERE employees.employersId is NULL