问题
I have many files, for example: 20170319, 20170320 ... For each file I have 2 columns, one for username and the other was data. I've created a table
create table A(user varchar(35), date date, data varchar(35), primary key(user, date));
Then, I want to load those files into database, and use filename as specific date in date field.
Can I still use sth like:
Load data infile '20170320' into table A
回答1:
The answer is that you cannot do this in MySQL alone, you need to use an external program or script that builds the load data infile
statements with the appropriate SET clause derived from the name of the file:
The SET clause can be used to supply values not derived from the input file. The following statement sets column3 to the current date and time:
LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, column2) SET column3 = CURRENT_TIMESTAMP;
The reason for this is that:
load data infile
cannot use the file name as an input variable- neither MySQL prepared statements using the prepare statement, nor stored procedures are allowed to use the
load data infile
statement.
来源:https://stackoverflow.com/questions/42923999/how-can-i-load-many-files-into-mysql-automatically-add-date-information