LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name'
[REPLACE | IGNORE]
INTO TABLE tbl_name
[PARTITION (partition_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 | ROWS}]
[(col_name_or_user_var,...)]
[SET col_name = expr,...]
starting by指定每行开始的位置,跟字符有关。
terminated by 指定列的分隔符,默认是Tab符(\t)。
enclosed by 指定列的包括符,默认为空。
escaped by 转义字符,默认的是反斜杠('\')。
IGNORE number {LINES | ROWS} :忽略数据文件的前N行。
如果你指定关键词low_priority,那么MySQL将会等到没有其他人读这个表的时候,才把插入数据。可以使用如下的命令:
load data low_priority infile "/tmp/data sql" into table Orders;
如果指定local关键词,则表明从客户主机读文件。如果local没指定,文件必须位于服务器上。
replace和ignore关键词控制对现有的唯一键记录的重复的处理。如果你指定replace,新行将代替有相同的唯一键值的现有行。
如果你指定ignore,跳过有唯一键的现有行的重复行的输入。如果你不指定任何一个选项,当找到重复键时,出现一个错误,并且文本文件的余下部分被忽略。例如:
load data low_priority infile "/tmp/data sql" replace into table Orders;
关于字符集的处理,
范例一:
load data infile '/tmp/test.txt' into table test charset gbk fields terminated by ',';
范例二:
先设置字符集,再执行导入语句:
set character_set_database=gbk;
表中的列比数据文件的列多,处理范例:
$more /tmp/test.csv
10,"zhangsan","zhangsan@qq.com",25
11,"lisi","lisi@qq.com",26
12,"wangwu","wangwu@qq.com",27
load data infile '/tmp/test.csv'
into table test
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
(id,name,ename,age);
表中的列比数据文件的列少,并且最后两列也不对应,处理范例:
$more /tmp/test.csv
10,"zhangsan",25,"zhangsan@qq.com",13000000000,0
11,"lisi",26,"lisi@qq.com",13000000001,5
12,"wangwu",27,"wangwu@qq.com",13000000002,2
load data infile '/tmp/test.csv'
into table test
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
(id,name,age,ename,@tmp_sex,@tmp_tel,status)
set sex-if(@tmp_sex='男',0,1;