Help us understand the problem. What is going on with this article?

csvをLOAD DATA INFILEするときよく発行するクエリ

More than 5 years have passed since last update.

たまに依頼があるLOAD DATA INFILEでの作業

event_history
CREATE TABLE event_hisotry(
  `id` bigint unsigned not null auto_increment,
  `member_id`, bigint unsigned not null,
  `event_id` tinyint unsigned not null,
  `created_at` datetime not null,
  PRIMARY KEY(`id`),
  KEY `key_1` (`member_id`, `timestamp1)
)Engine=InnoDB

みたいなテーブルに

"member_id","event_id","created_at"
"100","23","20130823100000"
"100","24","20130823101510"
"101","23","20130823095959"
...

みたいなcsvをいれてくれという依頼。

  • 1行目に取り込む必要がないものが入っている
  • 各fieldがダブルクォーテーションで囲まれている
  • auto_incrementのfieldが省略されている。

発行するクエリは

LOAD DATA INFILE '/data/mysql_backup/filename.csv' INTO TABLE event_hisotry 
  FIELDS TERMINATED BY ',' ENCLOSED BY '"' (member_id, event_id, `created_at`);

これでOK。

hdtkkj
XFLAG/mixi 所属。
mixi
全ての人に心地よいつながりを
http://mixi.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away