问题
I'm trying to import data into a MySQL table with values not defined in the input file. I have this php string concatenated query:
"LOAD DATA LOCAL INFILE '".@mysql_escape_string($this->file_name).
"' IGNORE INTO TABLE `".$this->table_name.
"` FIELDS TERMINATED BY '".@mysql_escape_string($this->field_separate_char).
"' OPTIONALLY ENCLOSED BY '".@mysql_escape_string($this->field_enclose_char).
"' ESCAPED BY '".@mysql_escape_string($this->field_escape_char).
"' LINES TERMINATED BY '". $this->line_separate_char .
"' ".
($this->use_csv_header ? " IGNORE 1 LINES " : "")
Is there a way I can set the account id in this load statement?
回答1:
As documented under LOAD DATA INFILE Syntax:
The
SET
clause can be used to supply values not derived from the input file. The following statement setscolumn3
to the current date and time:LOAD DATA INFILE 'file.txt' INTO TABLE t1 (column1, column2) SET column3 = CURRENT_TIMESTAMP;
回答2:
You can call functions and SQL statements within load data like this:
load data local infile '/tmp/foo.txt' into table foo
fields terminated by '\t' lines terminated by '\n'
(@col1, @col2)
set myid=@col1,
username=CONCAT(@col2, 'abc', 'xyz');
来源:https://stackoverflow.com/questions/19254610/can-you-define-fields-and-values-with-a-load-data-local-infile-query-in-mysql