select into outfile 语法

1
2
3
select email into outfile "test.txt" from email;

select substring(boss,0,2),addr from guest;
1
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE "/opt/abc.txt" INTO TABLE table_name FIELDS TERMINATED BY ',' (column1, column2,colum3);
1
2
3
mysql> SELECT * FROM table1 INTO OUTFILE 'data.txt'
FIELDS TERMINATED BY ','
FROM …

为了将由逗号分隔的文件读回来,正确的语句将是:

1
2
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
FIELDS TERMINATED BY ',';

相反,如果你试图用下面显示的语句读取文件,它不会工作,因为它命令LOAD DATA INFILE在字段之间寻找定位符:

1
2
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE table2
FIELDS TERMINATED BY '\t';

可能的结果是每个输入行将被解释为单个的字段。

LOAD DATA INFILE能被用来读取从外部来源获得的文件。例如,以dBASE格式的文件将有由逗号分隔并用双引号包围的字段。如果文件中的行由换行符终止,下面显示的命令说明你将用来装载文件的字段和行处理选项:

1
2
3
mysql> LOAD DATA INFILE 'data.txt' INTO TABLE tbl_name
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n';

任何字段或行处理选项可以指定一个空字符串(“)。如果不是空,FIELDS [OPTIONALLY] ENCLOSED BY和 FIELDS ESCAPED BY值必须是一个单个字符。FIELDS TERMINATED BY和LINES TERMINATED BY值可以是超过一个字符。例如,写入由回车换行符对(CR+LF)终止的行,或读取包含这样行的一个文件,指定一个LINES TERMINATED BY ‘\r \n’子句。

FIELDS [OPTIONALLY] ENCLOSED BY控制字段的包围字符。对于输出 (SELECT … INTO OUTFILE),如果你省略OPTIONALLY,所有的字段由ENCLOSED BY字符包围。对于这样的输出的一个例子(使用一个逗号作为字段分隔符)显示在下面:

“1”,”a string”,”100.20”
“2”,”a string containing a , comma”,”102.20”
“3”,”a string containing a \” quote”,”102.20”
“4”,”a string containing a \”, quote and comma”,”102.20”

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

1
insert into tabl(id,email) select id, email from guest;