(MySQL) Load data infile specific columns

2020-07-10 06:38发布

I have a problem loading data into specific columns of an table. The CSV file is build dynamic with the default fields ID, LAST_REFRESH, ALIAS1 and may contain ALIAS2 to ALIAS8. Current CSV only contains ALIAS1-4

The MySQL table contains the columns ID, LAST_REFRESH, ALIAS1-ALIAS8. My code for the first file already fails. Code after variables are set is:

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS (ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4)
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES

But unfortunately i still receive the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n'

Anybody knows what i'm doing wrong?

标签: mysql
2条回答
倾城 Initia
2楼-- · 2020-07-10 07:13

Remove the columns:

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
查看更多
▲ chillily
3楼-- · 2020-07-10 07:16

The column names have to be specified last. Read more about it here.

LOAD DATA LOCAL INFILE 'C:\\temp\\\OSS001'
INTO TABLE REJECTS 
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(ID, REFRESH_DATE, ALIAS1, ALIAS2, ALIAS3, ALIAS4, ALIAS5, ALIAS6, ALIAS7, ALIAS8)
查看更多
登录 后发表回答