Pregunta

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?

¿Fue útil?

Solución

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;

Otros consejos

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');
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top