将数据加载到 mySQL 并忽略特定列

Load data to mySQL and ignore specific columns

我正在尝试将 csv 文件加载到 mySQL。我想忽略 3 列,所以我使用“@”字符来实现。似乎在我忽略的第一列(命名为:'ignore1')之后除了数据之外的所有内容都在上传。

这是 mySQL 软件的错误吗?

   LOAD DATA LOCAL INFILE 'Z:/test.csv'  
    INTO TABLE IN_Apolo
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 ROWS
    (name, @date, @ignore1, @ignore2, @ignore3, column3, column4)
    set date = str_to_date(@date,'%Y-%m-%d')
    ;

column3column4 似乎为 NULL

更新:

我尝试使用此代码并且有效:

   LOAD DATA LOCAL INFILE 'Z:/test.csv'  
    INTO TABLE IN_Apolo
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY '\n'
    IGNORE 1 ROWS
    (@name, @date, @column3, @column4)
    set name=@name, date = str_to_date(@date,'%Y-%m-%d'), column3=@column3, column4=@column4     ;

但它应该也适用于第一个版本。

LOAD DATA LOCAL INFILE 'Z:/test.csv' 
INTO TABLE IN_Apolo 
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(@name, @date,@column3, @column4)
set name=@name,date=str_to_date(@date,'%Y-%m-%d'),column3=@column3,column4=@column4;

尝试使用这个...

Table:

DROP TABLE IF EXISTS `test_table`;

CREATE TABLE `test_table` (
    `name` VARCHAR(20) DEFAULT NULL,
    `date` DATE DEFAULT NULL,
    `ignore_1` VARCHAR(20) DEFAULT NULL,
    `ignore_2` VARCHAR(20) DEFAULT NULL,
    `ignore_3` VARCHAR(20) DEFAULT NULL,
    `column_3` VARCHAR(20) DEFAULT NULL,
    `column_4` VARCHAR(20) DEFAULT NULL
) ENGINE=InnoDB;

文件:

/path/to/test_file.csv

"name","date","ignore_1","ignore_2","ignore_3","column_3","column_4"
"name_0","2000-01-01","ignore_1_0","ignore_2_0","ignore_3_0","column_3_0","column_4_0"
"name_1","2000-01-02","ignore_1_1","ignore_2_1","ignore_3_1","column_3_1","column_4_1"
"name_2","2000-01-03","ignore_1_2","ignore_2_2","ignore_3_2","column_3_2","column_4_2"

MySQL 命令行:

mysql> LOAD DATA INFILE '/path/to/test_file.csv'
       INTO TABLE `test_table`
       FIELDS TERMINATED BY ','
       ENCLOSED BY '"'
       LINES TERMINATED BY '\n'
       IGNORE 1 ROWS
       (`name`, @`date`, @`ignore_column`, @`ignore_column`, @`ignore_column`, `column_3`, `column_4`)
       SET `date` = STR_TO_DATE(@`date`, '%Y-%m-%d');