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

怎甘沉沦 提交于 2019-12-13 07:13:37

问题


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 sets column3 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!