Search code examples
mysqlload-data-infile

Is there a way to insert custom data while loading data from a file in MySQL?


I am using the following statement to load data from a file into a table:

LOAD DATA LOCAL INFILE '/home/100000462733296__Stats" 
INTO TABLE datapoints 
FIELDS TERMINATED BY '|' 
LINES TERMINATED BY '\n' 
(uid1, uid2, status);

Now, if I want to enter a custom value into uid1, say 328383 without actually asking it to read it from a file, how would I do that? There are about 10 files and uid1 is the identifier for each of these files. I am looking for something like this:

LOAD DATA LOCAL INFILE '/home/100000462733296__Stats" 
INTO TABLE datapoints 
FIELDS TERMINATED BY '|' 
LINES TERMINATED BY '\n' 
(uid1="328383", uid2, status);

Any suggestions?


Solution

  • The SET clause can be used to supply values not derived from the input file:

    LOAD DATA LOCAL INFILE '/home/100000462733296__Stats" 
    INTO TABLE datapoints 
    FIELDS TERMINATED BY '|' 
    LINES TERMINATED BY '\n' 
    (uid1, uid2, status)
    SET uid1 = '328383';
    

    It's not clear what the data type of uid1 is, but being that you enclosed the value in double quotes I assumed it's a string related data type - remove the single quotes if the data type is numeric.

    There's more to read on what the SET functionality supports in the LOAD FILE documentation - it's a little more than 1/2 way down the page.