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’;

 

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

 

 

 

select sx,mz,bz from cr01 into OUTFILE 'C:/Users/del/Desktop/a.txt'
fields terminated by '、'optionally enclosed by '\"' lines starting by '\>' terminated by '\r\n';
字段的分隔符 字符上加括號 開頭 結尾換行



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

   

 

 

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

前期數據准備:

create table cr01 ( sx int(50),
                    mz varchar(50),
                    bz varchar(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;

 

 

 

 

 

 

 

 

單表導出為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'

 

 

 

 

 

 

 

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

 

create table employee ( num int(50),
                        d_id int(50),
                        name varchar(50),
                        age int(50),
                        sex varchar(50),
                        homeadd varchar(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 * from employee;

create table department ( d_id int(50),
                          d_name varchar(50),
                          functione varchar(50),
                          address varchar(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 * from employee;

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_id
into OUTFILE 'C:/Users/del/Desktop/a.xls';

 

 

 

 

 

 

 

 

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

右連接:

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_id
into OUTFILE 'C:/Users/del/Desktop/a.xls';

 

 

 

 

 

 

 

 

 

 

 

 

 

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

導出文本文件:

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

 

 

>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 terminated by '\t'optionally enclosed by ' ' lines starting by ' ' terminated by '\r\n';
 1     sww      sww01 
 2     aww      aww02 
 3     qww      qww03 
 4     eww      eww04 
 5     rww      rww05 
 6     yww      yww06 
 7     uww      uww07 

 

 

 

 

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

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

 

 

 

 

 

 

 

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

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

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM