仅创建表中不存在的记录

时间:2015-04-13 09:48:33

标签: mysql

USE Table;
LOAD DATA LOCAL INFILE 'C:/file.csv' 
INTO TABLE TABLE1 
FIELDS TERMINATED BY ';' 
LINES TERMINATED BY '\r\n' 
(, Field1, Field2, Field3);

我的问题是,是否会跳过先前存在于表中的所有记录,并且只会创建不存在的记录?

1 个答案:

答案 0 :(得分:0)

为此,您的表必须具有主键或唯一索引。您导入的数据也必须将数据插入此列。然后可以识别现有行。您必须添加IGNORE关键字 详细了解IGNORE here

LOAD DATA LOCAL INFILE 'C:/file.csv' 
IGNORE
INTO TABLE TABLE1 
FIELDS TERMINATED BY ';' 
LINES TERMINATED BY '\r\n' 
(Field1, Field2, Field3);

从手册:

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
    [REPLACE | IGNORE] /*don't confuse this one...*/
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number LINES] /*...with this one*/
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]