这篇文章主要介绍“MySQL怎么导入数据”,在日常操作中,相信很多人在mysql怎么导入数据问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”mysql怎么导入数据”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的惠农网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
mysqlimport
示例
mysqlimport -uroot -p123456 test /tmp/mytbl.txt;
约定:文件名的最后一部分为表名,以上语句导入到表mytbl
mysqlimport必须指定数据库,以上语句数据库为test
www.2cto.com
导入csv
mysqlimport -uroot -p --local --lines-terminated-by="\r\n" --fields-terminated-by="," --fields-enclosed-by="\"" test /tmp/mytbl.csv
load data
示例
mysql> load data infile '/tmp/mytbl.txt' into table mytbl
load data可以不指定数据库
以上语句中,mysql必须有/tmp/的读权限
导入csv
mysql> load data infile '/tmp/mytbl.csv' into table mytbl fields terminated by ',' enclosed by '"' lines terminated by '\r\n'
www.2cto.com
处理重复主键
替换已有值
mysql> load data infile '/tmp/mytbl.txt' replace into table mytbl fields terminated by '\t' lines terminated by '\n'
表中已有则不导入
mysql> load data infile '/tmp/mytbl.csv' ignore into table mytbl fields terminated by ',' enclosed by '"' lines terminated by '\r\n'
跳过文件行
以下示例为跳过第一行
mysql> load data infile '/tmp/mytbl.txt' into table mytbl ignore 1 lines;
预处理
文件data.txt内容:
DateTimeNameWeightState
2006-09-0112:00:00Bill Wills200Nevada
2006-09-0209:00:00Jeff Deft150Oklahoma
2006-09-0403:00:00Bob Hobbs225Utah
2006-09-0708:00:00Hank Banks175Texas
文件必须被加载入如下的表
create table tbl
(
dt datetime,
last_name char(10),
first_name char(10),
weight_kg float,
st_abbrev char(2)
)
create table states
(
name varchar(20),
shortname char(2)
) www.2cto.com
states 表中内容:
name shortname
Nevada NV
Oklahoma OK
Utah UT
Texas TX
insert into states
values('Nevada','NV')
,('Oklahoma','OK')
,('Utah', 'UT')
,('Texas', 'TX')
导入:
load data infile '/tmp/data.txt' into table tbl
ignore 1 lines
(@date,@time,@name,@weight_lb,@state)
set dt=concat(@date,' ',@time),
first_name=substring_index(@name,' ',1),
last_name=substring_index(@name,' ',-1),
weight_kg=@weight_lb * .454,
st_abbrev = (select shortname from states where name=@state);
结果:
mysql> select * from tbl; www.2cto.com +---------------------+-----------+------------+-----------+-----------+
| dt | last_name | first_name | weight_kg | st_abbrev |
+---------------------+-----------+------------+-----------+-----------+
| 2006-09-01 12:00:00 | Wills | Bill | 90.8 | NV |
| 2006-09-02 09:00:00 | Deft | Jeff | 68.1 | OK |
| 2006-09-04 03:00:00 | Hobbs | Bob | 102.15 | UT |
| 2006-09-07 08:00:00 | Banks | Hank | 79.45 | TX |
+---------------------+-----------+------------+-----------+-----------+
将本地文件导入到linux下的mysql,加local
load data local infile 'C:/mytbl.txt' into table mytbl fields terminated by '\t' lines terminated by '\r\n'
到此,关于“mysql怎么导入数据”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
文章标题:mysql怎么导入数据
文章转载:http://lswzjz.com/article/ijhioi.html