load data local infile "txt文件路径"
into table 表名
fields terminated by ","
lines terminated by "\n";
参数
描述
load data local infile “txt文件路径”
绝对路径(双/单引号包起来)
fields terminated by “,”
字段分隔符
lines terminated by “\n”;
行分隔符
1、表字段数>txt字段数
表字段
id,name,age
txt字段
id,name,address,age
load data local infile "c:/Users/xiaox/Desktop/test.txt"
into table test
fields terminated by ","
lines terminated by "\n"(id,name,@abc,age);
@abc表示变量,使用变量作为占位符,导入时等于丢掉这列数据。
2、表字段数<txt字段数
表字段
id,name,age
txt字段
name,age
load data local infile "c:/Users/xiaox/Desktop/test.txt"
into table test
fields terminated by ","
lines terminated by "\n"(name,age);
二、示例
C:\Users\xiaox>mysql -h localhost -u root -p xiaoxian --local-infile
Enter password:******WelcometotheMySQLmonitor. Commands end with; or \g.
YourMySQL connection id is 22Server version:8.0.26MySQLCommunityServer-GPLCopyright(c)2000,2021,Oracle and/or its affiliates.
Oracle is a registered trademark of OracleCorporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type'help;' or '\h'forhelp. Type'\c'toclear the current input statement.
mysql> load data local infile "c:/Users/xiaox/Desktop/test.txt" into table test (id,area);QueryOK,2 rows affected,4 warnings (0.01 sec)Records:2Deleted:0Skipped:0Warnings:4
mysql> load data local infile "c:/Users/xiaox/Desktop/test.txt" into table test fields terminated by ",";QueryOK,0 rows affected,2 warnings (0.01 sec)Records:2Deleted:0Skipped:2Warnings:2
mysql> load data local infile "c:/Users/xiaox/Desktop/test.txt" into table test fields terminated by ","(area,id);QueryOK,2 rows affected (0.01 sec)Records:2Deleted:0Skipped:0Warnings:0
三、错误
1、ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version