Can you define fields and values with a "LOAD DATA LOCAL INFILE" query in MySQL?

StackOverflow https://stackoverflow.com/questions/19254610

  •  30-06-2022
  •  | 
  •  

سؤال

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?

هل كانت مفيدة؟

المحلول

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 sets column3 to the current date and time:

LOAD DATA INFILE 'file.txt'
  INTO TABLE t1
  (column1, column2)
  SET column3 = CURRENT_TIMESTAMP;

نصائح أخرى

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');
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top