mysql select 导出 文本_mysql——表的导出——用select……into outfile导出 xls文件和文本文件...

可以使用select……into  outfile导出txt、excel等格式的文件

语法格式: select  [ 列名 ]  from  table  [ where  语句 ]  into  outfile  ‘目标文件’  [  option ];

语句可以分为两个部分,前面是一个普通的select查询语句,通过这个语句查询处所需要的数据;

后部分是导出数据导到哪里,以及导出数据的格式;

------------------------------------------------------------------------------------------------------------------------------------

‘目标文件’:指将查出的记录导出到哪个文件;

option:有常用的5个选项,分别如下:

fields  terminated  by  '字符串':设置字符串为字段的分隔符,默认值是‘ \t ’;

fields  enclosed  by  '字符':设置字符来括上字段的值。默认情况下不使用任何符号;

fields  optionally  enclosed  by  '字符':设置字符来括上char,varchar,text等字符型字段,默认情况下不使用任何符号;

fields  escaped  by '字符':设置转义字符,默认值为“\”;

lines  starting  by  '字符串':设置每行开头的字符,默认情况下无任何字符;

lines  terminated  by  '字符串':设置每行的结束符,默认值是‘\n’;

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

6f2c37fa9c46da6b7e7bf9ad7ed04a62.png

select sx,mz,bz from cr01 into OUTFILE 'C:/Users/del/Desktop/a.txt'fields terminatedby '、'optionally enclosed by '\"' lines starting by '\>' terminated by '\r\n';

字段的分隔符 字符上加括号 开头 结尾换行

PS:Windows下: \r\n 是换行

b2a4c214b8704e24265fd03022d338cc.png

------------------------------------------------------------------------------------------------------------------------------

前期数据准备:

create table cr01 ( sx int(50),

mzvarchar(50),

bzvarchar(50)

);insert into cr01 ( sx,mz,bz ) values (1,'sww','sww01');insert into cr01 values (2,'aww','aww02');insert into cr01 values (3,'qww','qww03'),

(4,'eww','eww04'),

(5,'rww','rww05');insert into cr01 ( sx,mz,bz ) values (6,'yww','yww06'),

(7,'uww','uww07');select * from cr01;

6877fcc34399a6de907d95a63c4f0557.png

单表导出为xls文件,导出到桌面:

select sx,mz,bz from cr01 into OUTFILE 'C:/Users/del/Desktop/a.xls'

示例:select * from cr01 where sx in (1,3,5,7) into OUTFILE 'C:/Users/del/Desktop/a.xls'

ebc49f05c2b3da36429be89716a8fa98.png

1e9af6f7b8b153c6871efc3bb53460f9.png

===========================================================================================

create table employee ( num int(50),

d_idint(50),

namevarchar(50),

ageint(50),

sexvarchar(50),

homeaddvarchar(50)

);insert into employee values(1,1001,'zhangsan',26,'nan','beijing');insert into employee values(2,1001,'lisi',24,'nv','hunan');insert into employee values(3,1002,'wangwu',25,'nan','jiangsu');insert into employee values(4,1004,'aric',15,'nan','yingguo');select * fromemployee;create table department ( d_id int(50),

d_namevarchar(50),

functionevarchar(50),

addressvarchar(50)

);insert into department values(1001,'keyanbu','yanfachanpin','3lou5hao');insert into department values(1002,'shengchanbu','shengchanchanp','5louyiceng');insert into department values(1003,'xiaoshoubu','cehuaxiaoshou','1louxiaoshoudating');select * fromemployee;select * from department;

================================================================================================

多表导出为xls文件:(左连接)

select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id =department.d_idinto OUTFILE 'C:/Users/del/Desktop/a.xls';

68580be7847dcd77fa146c8a0c48e60a.png

459a3f0f83876f146de2929e71ecb9fd.png

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

右连接:

select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id = department.d_id;

select num,name,employee.d_id,age,sex,d_name,functione from employee right join department on employee.d_id =department.d_idinto OUTFILE 'C:/Users/del/Desktop/a.xls';

b4cfd1a672287416d1d708b1df7de480.png

040354e0cad66f9cb0d1611b13f4d806.png

17ed270958d494c6e3b629441f33fb10.png

========================================================================

导出文本文件:

select sx,mz,bz from cr01 into OUTFILE 'C:/Users/del/Desktop/a.txt'fields terminatedby '\、' optionally enclosed by '\“' lines starting by '\>' terminated by '\r\n';

e5b00de9d86cc439800dd52c50735db7.png

0d39d5f4b427f97482f9179c870c1a4c.png

>1、“sww“、“sww01“>2、“aww“、“aww02“>3、“qww“、“qww03“>4、“eww“、“eww04“>5、“rww“、“rww05“>6、“yww“、“yww06“>7、“uww“、“uww07“

=========================================================================================

修改参数:

select sx,mz,bz from cr01 into OUTFILE 'C:/Users/del/Desktop/a.txt'fields terminatedby '\t'optionally enclosed by ' ' lines starting by ' ' terminated by '\r\n';

1sww sww012aww aww023qww qww034eww eww045rww rww056yww yww067 uww uww07

++++++++++++++++++++++++++++++++++++++++++++++++++++++++

select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id =department.d_idinto OUTFILE 'C:/Users/del/Desktop/a.txt'fields terminatedby '\t'optionally enclosed by ' ' lines starting by ' ' terminated by '\r\n';

0127bb6213f7302bfafb8b3f4ac3552f.png

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

select num,name,employee.d_id,age,sex,d_name,functione from employee left join department on employee.d_id =department.d_idinto OUTFILE 'C:/Users/del/Desktop/b.txt'fields terminatedby '\t'optionally enclosed by ' ' lines starting by ' ' terminated by '\r\n';

670afddc1c778ff59d5309a85cac1ece.png

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值