Search code examples
phpmysqlload-data-infile

Retrieving ID's of imported CSV (LOAD DATA INFILE)


I have the following scenario:

I have to import a CSV file to a table. For this i use the LOAD DATA LOCAL INFILE.

Is there a way to retrieve the id's (column 'id') of all inserted rows, since i need them in next step.


Solution

  • Create an BEFORE INSERT or AFTER INSERT trigger and store inserted ID values into another table -

    CREATE TRIGGER trigger1
    AFTER INSERT
    ON table1
    FOR EACH ROW
    BEGIN
      INSERT INTO table2(id) VALUES (NEW.id);
    END