的SELECT ...
INTO
形式SELECT
使查询结果可以存储在变量中或写入文件:
SELECT ... INTO
选择列值并将它们存储到变量中。var_list
SELECT ... INTO OUTFILE
将选定的行写入文件。可以指定列和行终止符以生成特定的输出格式。SELECT ... INTO DUMPFILE
将单行写入文件,不带任何格式。
一个给定的SELECT
语句最多可以包含一个INTO
子句,尽管如SELECT
语法描述所示(请参阅第 13.2.9 节,“SELECT 语句”),它们
INTO
可以出现在不同的位置:
之前
FROM
。例子:SELECT * INTO @myvar FROM t1;
在尾随锁定子句之前。例子:
SELECT * FROM t1 INTO @myvar FOR UPDATE;
INTO
不应在嵌套中使用子句,
因为SELECT
这样
的子句SELECT
必须将其结果返回到外部上下文。INTO
within
UNION
语句的使用也有限制
;参见
第 13.2.9.3 节,“UNION 子句”。
对于变体:
INTO
var_list
var_list
命名一个或多个变量的列表,每个变量可以是用户定义的变量、存储过程或函数参数,或存储程序局部变量。(在准备好的 语句中,只允许使用用户定义的变量;请参阅 第 13.6.4.2 节,“局部变量范围和解析”。)SELECT ... INTO
var_list
所选值被分配给变量。变量数必须与列数匹配。查询应返回一行。如果查询未返回任何行,则会出现错误代码为 1329 的警告 (
No data
),并且变量值保持不变。如果查询返回多行,则会出现错误 1172 (Result consisted of more than one row
)。如果语句可能检索多行,则可以使用LIMIT 1
将结果集限制为单行。SELECT id, data INTO @x, @y FROM test.t1 LIMIT 1;
用户变量名称不区分大小写。请参阅 第 9.4 节,“用户定义的变量”。
将所选行写入文件
的形式
。该文件是在服务器主机上创建的,因此您必须具有使用此语法的权限。不能是现有文件,除其他事项外,这会防止
修改数据库表等文件。系统变量控制文件名的
解释。
SELECT ... INTO
OUTFILE '
file_name
'SELECT
FILE
file_name
/etc/passwd
character_set_filesystem
该SELECT ... INTO
OUTFILE
语句旨在启用将表转储到服务器主机上的文本文件。在其他主机上创建生成的文件
SELECT ... INTO
OUTFILE
通常是不合适的,因为无法写入相对于服务器主机文件系统的文件路径,除非可以使用网络访问远程主机上的文件位置-服务器主机文件系统上的映射路径。
或者,如果远程主机上安装了 MySQL 客户端软件,您可以使用客户端命令,例如在该主机上生成文件。
mysql
-e "SELECT ..." >
file_name
SELECT ... INTO
OUTFILE
is the complement of LOAD
DATA
. Column values are written converted to the
character set specified in the CHARACTER SET
clause. If no such clause is present, values are dumped using
the binary
character set. In effect, there is
no character set conversion. If a result set contains columns in
several character sets, so does the output data file and it may
not be possible to reload the file correctly.
The syntax for the export_options
part of the statement consists of the same
FIELDS
and LINES
clauses
that are used with the LOAD DATA
statement. For information about the FIELDS
and LINES
clauses, including their default
values and permissible values, see Section 13.2.6, “LOAD DATA Statement”.
FIELDS ESCAPED BY
controls how to write
special characters. If the FIELDS ESCAPED BY
character is not empty, it is used when necessary to avoid
ambiguity as a prefix that precedes following characters on
output:
The
FIELDS ESCAPED BY
characterThe
FIELDS [OPTIONALLY] ENCLOSED BY
characterFIELDS TERMINATED BY
和LINES TERMINATED BY
值 的第一个字符ASCII
NUL
(零值字节;转义符后面实际写的是 ASCII0
,不是零值字节)
、FIELDS TERMINATED BY
、或字符必须ENCLOSED
BY
进行转义,以便您可以可靠地读回文件。ASCII
被转义以使其更易于使用某些寻呼机查看。
ESCAPED BY
LINES
TERMINATED BY
NUL
生成的文件不需要符合 SQL 语法,因此不需要转义任何其他内容。
If the FIELDS ESCAPED BY
character is empty,
no characters are escaped and NULL
is output
as NULL
, not \N
. It is
probably not a good idea to specify an empty escape character,
particularly if field values in your data contain any of the
characters in the list just given.
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
If you use INTO DUMPFILE
instead of
INTO OUTFILE
, MySQL writes only one row into
the file, without any column or line termination and without
performing any escape processing. This is useful for selecting a
BLOB
value and storing it in a
file.
Any file created by INTO OUTFILE
or
INTO DUMPFILE
is writable by all users on
the server host. The reason for this is that the MySQL server
cannot create a file that is owned by anyone other than the
user under whose account it is running. (You should
never run mysqld as
root
for this and other reasons.) The file
thus must be world-writable so that you can manipulate its
contents.
If the secure_file_priv
system variable is set to a nonempty directory name, the file
to be written must be located in that directory.
在作为事件计划程序执行的事件的一部分出现的语句的上下文中
SELECT ...
INTO
,诊断消息(不仅是错误,还有警告)被写入错误日志,并且在 Windows 上,写入应用程序事件日志。有关其他信息,请参阅第 23.4.5 节,“事件调度程序状态”。