mysqlcsvload-data-infileimport-csv

mysql loading csv file plus additional columns into a table


I am reading articles and thread on how to load a csv file into a table, but I also need to add additional columns. Is this possible?

Below is my table:

+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| uid       | int(11)     | NO   | PRI | NULL    | auto_increment |
| lid       | int(11)     | YES  |     | NULL    |                |
| uDate     | date        | YES  |     | NULL    |                |
| cID       | int(11)     | YES  |     | NULL    |                |
| active    | int(1)      | YES  |     | 0       |                |
| user_ID   | varchar(32) | YES  |     | NULL    |                |

above are additional columns I need to pass in, below are all the columns in the CSV file

| fName     | varchar(32) | YES  |     | NULL    |                |
| lName     | varchar(32) | YES  |     | NULL    |                |
| lAddress1 | varchar(64) | YES  |     | NULL    |                |
| lAddress2 | varchar(16) | YES  |     | NULL    |                |
| lCity     | varchar(64) | YES  |     | NULL    |                |
| lState    | varchar(64) | YES  |     | NULL    |                |
| lZip      | varchar(10) | YES  |     | NULL    |                |
| lPhone    | varchar(15) | YES  |     | NULL    |                |
| lEmail    | varchar(96) | YES  |     | NULL    |                |
| field1    | varchar(32) | YES  |     | NULL    |                |
| field2    | varchar(64) | YES  |     | NULL    |                |
| field3    | varchar(64) | YES  |     | NULL    |                |
| regCode   | varchar(32) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

And to load the file:

LOAD DATA LOCAL INFILE '/path/to/file/myfile.csv' INTO TABLE myTable
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
(fName,lName,lAddress1,lAddress2,lCity,lState,lZip,field1,field2,field3,regCode) ;

UPDATE

How can I also pass in lid,uDate,cID,active,user_ID ?

These fields are:

lid = listID 
cID = customerID 
active = defaults to inactive, but entrycan be later activated  
user_ID = initially null, but later updated with a properly assigned userID.

In my php script, they are already collected as $lid,$cID,0,$userID


Solution

  • You can add a SET clause to set additional columns other than the ones in the CSV.

    $stmt = $conn->prepare("
        LOAD DATA LOCAL INFILE '/path/to/file/myfile.csv' INTO TABLE myTable
        FIELDS TERMINATED BY ',' 
        ENCLOSED BY '\"' 
        LINES TERMINATED BY '\r\n'
        (fName,lName,lAddress1,lAddress2,lCity,lState,lZip,field1,field2,field3,regCode)
        SET lid = ?, cID = ?, active = 0, user_id = ?");
    $stmt->bind_param("iis", $lid, $cID, $userID);
    $stmt->execute();