一尘不染

MYSQL LOAD DATA INFILE忽略重复的行(自动增量作为主键)

mysql

我在使用LOAD DATA INFILE命令时遇到了一些麻烦,因为我想忽略数据库中已经存在的行。如果说我的数据表如下,

id  |name   |age
--------------------
1   |aaaa   |22
2   |bbbb   |21
3   |bbaa   |20
4   |abbb   |22
5   |aacc   |22

其中id是自动递增值。我拥有的csv文件包含以下数据,

"cccc","14"
"ssee","33"
"dddd","22"
"aaaa","22"
"abbb","22"
"dhgg","34"
"aacc","22"

我想忽略这些行,

    "aaaa","22"
    "abbb","22"
    "aacc","22"

并将其余的上传到表格中。我尚未将所有内容上传到表的查询如下,

    LOAD DATA INFILE 'member.csv'
    INTO TABLE tbl_member
    FIELDS TERMINATED BY ','
           ENCLOSED BY '"'
           ESCAPED BY '"'
           LINES TERMINATED BY '\n'
    (name, age);

请帮助我完成此任务。。将不胜感激..我尝试了许多链接,但没有帮助:(


阅读 1115

收藏
2020-05-17

共1个答案

一尘不染

在年龄列上创建一个唯一索引,然后:

LOAD DATA INFILE 'member.csv'
IGNORE INTO TABLE tbl_member
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
(name, age);
2020-05-17