加載數據的例子CSV
Original post: http://anothermysqldba.blogspot.com/2013/05/load-data-example.html 我看到最近的一個問題,所以我想我會在這裡發表我的解決方案的例子,以及有關LOAD DATA上forums.mysql.com網站。
有問題的用戶得到了很多跳過行沒有警告。 用戶也想跳過標題行,我以為設置一些領域,因為它是進口的。 由於我沒有看到相關的任何數據或模式,我只是發表了以下的工作的例子:
CREATE TABLE example (
Id int(11) NOT NULL AUTO_INCREMENT,
Column2 varchar(14) NOT NULL,
Column3 varchar(14) NOT NULL,
Column4 varchar(14) NOT NULL,
Column5 DATE NOT NULL,
PRIMARY KEY (Id)
) ENGINE=InnoDB
Column1 Column2 Column3 Column4 Column5 1 A Foo sdsdsd 4/13/2013 2 B Bar sdsa 4/12/2013 3 C Foo wewqe 3/12/2013 4 D Bar asdsad 2/1/2013 5 E FOObar wewqe 5/1/2013
more /tmp/example.csv
Column1,Column2,Column3,Column4,Column5 1,A,Foo,sdsdsd,4/13/2013 2,B,Bar,sdsa,4/12/2013 3,C,Foo,wewqe,3/12/2013 4,D,Bar,asdsad,2/1/2013 5,E,FOObar,wewqe,5/1/2013
LOAD DATA LOCAL INFILE '/tmp/example.csv' -> INTO TABLE example -> FIELDS TERMINATED BY ',' -> LINES TERMINATED BY '\n' -> IGNORE 1 LINES -> (id, Column2, Column3,Column4, @Column5) -> set -> Column5 = str_to_date(@Column5, '%m/%d/%Y'); Query OK, 5 rows affected (0.04 sec) Records: 5 Deleted: 0 Skipped: 0 Warnings: 0
select * from example; +----+---------+---------+---------+------------+ | Id | Column2 | Column3 | Column4 | Column5 | +----+---------+---------+---------+------------+ | 1 | A | Foo | sdsdsd | 2013-04-13 | | 2 | B | Bar | sdsa | 2013-04-12 | | 3 | C | Foo | wewqe | 2013-03-12 | | 4 | D | Bar | asdsad | 2013-02-01 | | 5 | E | FOObar | wewqe | 2013-05-01 | +----+---------+---------+---------+------------+ 5 rows in set (0.00 sec)