2011-03-09 82 views
0

如何獲取LOAD DATA INFILE命令以跳過某些字段。SQL LOAD DATA INFILE問題

可以說我有如下表:

CREATE TABLE stuff (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL, 
    name CHAR(64) NOT NULL, 
    junk CHAR(64) NOT NULL, 
    a ENUM('N','Y') NOT NULL DEFAULT 'N', 
    PRIMARY KEY (id) 
); 

我的文件:

,name1,junk1, 
,name2,junk2, 
,name3,junk3,Y 

不聲明:

LOAD DATA INFILE 'myfile' INTO TABLE stuff FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'; 

自動跳過自動遞增,並處理它正常嗎?它似乎處理一切正常,但這會產生以下警告:

Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 1 
Warning (Code 1265): Data truncated for column 'a' at row 1 
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 2 
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 3 
Warning (Code 1265): Data truncated for column 'a' at row 3 
Warning (Code 1366): Incorrect integer value: '' for column 'id' at row 4 

在開始時刪除逗號不會正確輸入數據。

回答

2

您可以明確地將id設置爲NULL,以便它使用自動遞增值。

LOAD DATA INFILE 'myfile' 
INTO TABLE stuff 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n' 
SET id = NULL; 

我也建議刪除第一個逗號,並明確列出在文件中的列:

LOAD DATA INFILE 'myfile' 
INTO TABLE stuff 
FIELDS TERMINATED BY ',' 
LINES TERMINATED BY '\n' 
(name,junk,a) 
SET id = NULL;