mysql outfile xls_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’;

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

b30718b4cab80b41bab46d648bd6ee8b.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;

dbdd6f9cea69fb57fe3dea58b77fc47e.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'

c686d2ad8a8920c120088d6afb72c7b9.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';

fc34e82dcbf04aff5e4b79d396d0a832.png

8d54c8e2be11d5b6201e81fd49444f74.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';

dd8856f6c05aacf78fa4e8a072ed1188.png

1e59b25865b7d9e7a3f8bfb434899f9a.png

6fd8fa9886c2e7614dec4ce3a3f7b4fe.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';

0bc06728dc66eeaba6ef1baaaee52e5a.png

8011ceef8c33318fe4f598f5b16357b9.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';

1eab38eacbf972b3532af9638752e6a8.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';

48ca1517e17046d111777270e049ece7.png

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值