The SELECT ... INTO
form of SELECT
enables a query result to be stored in variables or written to a file:SELECT
语句的SELECT ... INTO
形式可以将查询结果存储在变量中或写入文件中:
SELECT ... INTO
var_list
selects column values and stores them into variables.选择列值并将其存储到变量中。
SELECT ... INTO OUTFILE
writes the selected rows to a file. 将所选行写入文件。Column and line terminators can be specified to produce a specific output format.可以指定列和行终止符以生成特定的输出格式。
SELECT ... INTO DUMPFILE
writes a single row to a file without any formatting.将单行不带任何格式写入文件。
A given 给定的SELECT
statement can contain at most one INTO
clause, although as shown by the SELECT
syntax description (see Section 13.2.10, “SELECT Statement”), the INTO
can appear in different positions:SELECT
语句最多可以包含一个INTO
子句,尽管如SELECT
语法描述所示(参见第13.2.10节,“SELECT语句”),INTO
可以出现在不同的位置:
Before FROM
. FROM
前面。Example:例如:
SELECT * INTO @myvar FROM t1;
Before a trailing locking clause. 在尾锁子句之前。Example:例如:
SELECT * FROM t1 INTO @myvar FOR UPDATE;
At the end of the 在SELECT
. SELECT
的末尾。Example:例如:
SELECT * FROM t1 FOR UPDATE INTO @myvar;
The MySQL 8.0.20支持语句末尾的INTO
position at the end of the statement is supported as of MySQL 8.0.20, and is the preferred position. INTO
位置,这是首选位置。The position before a locking clause is deprecated as of MySQL 8.0.20; expect support for it to be removed in a future version of MySQL. 从MySQL8.0.20开始,锁定子句之前的位置已被弃用;在MySQL的未来版本中,对它的支持可能会被删除。In other words, 换句话说,INTO
after FROM
but not at the end of the SELECT
produces a warning.INTO
在FROM
后面但不在SELECT
的末尾会产生警告。
An 嵌套INTO
clause should not be used in a nested SELECT
because such a SELECT
must return its result to the outer context. SELECT
中不应使用INTO
子句,因为这样的SELECT
必须将其结果返回到外部上下文。There are also constraints on the use of 在INTO
within UNION
statements; see Section 13.2.10.3, “UNION Clause”.UNION
语句中使用INTO
也有限制;请参阅第13.2.10.3节,“UNION子句”。
For the 对于INTO
variant:var_list
INTO
变量:var_list
var_list
names a list of one or more variables, each of which can be a user-defined variable, stored procedure or function parameter, or stored program local variable. var_list
命名了一个或多个变量的列表,每个变量可以是用户定义的变量、存储过程或函数参数或存储程序局部变量。(Within a prepared (在准备好的SELECT ... INTO
statement, only user-defined variables are permitted; see Section 13.6.4.2, “Local Variable Scope and Resolution”.)var_list
SELECT ... INTO
语句中,只允许用户定义变量;请参阅第13.6.4.2节,“局部变量范围和分辨率”。)var_list
The selected values are assigned to the variables. 选定的值将分配给变量。The number of variables must match the number of columns. 变量数必须与列数匹配。The query should return a single row. 查询应返回一行。If the query returns no rows, a warning with error code 1329 occurs (如果查询不返回任何行,则会出现错误代码为1329的警告(No data
), and the variable values remain unchanged. No data
),并且变量值保持不变。If the query returns multiple rows, error 1172 occurs (如果查询返回多行,则出现错误1172(结果由多行构成)。Result consisted of more than one row
). If it is possible that the statement may retrieve multiple rows, you can use 如果语句可能检索多行,可以使用LIMIT 1
to limit the result set to a single row.LIMIT 1
将结果集限制为一行。
SELECT id, data INTO @x, @y FROM test.t1 LIMIT 1;
INTO
can also be used with a var_list
TABLE
statement, subject to these restrictions:INTO
也可以与var_list
TABLE
语句一起使用,但有以下限制:
The number of variables must match the number of columns in the table.变量数必须与表中的列数匹配。
If the table contains more than one row, you must use 如果表包含多行,则必须使用LIMIT 1
to limit the result set to a single row. LIMIT 1
将结果集限制为一行。LIMIT 1
must precede the INTO
keyword.LIMIT 1
必须在INTO
关键字之前。
An example of such a statement is shown here:这种说法的一个例子如下:
TABLE employees ORDER BY lname DESC LIMIT 1 INTO @id, @fname, @lname, @hired, @separated, @job_code, @store_id;
You can also select values from a 也可以从VALUES
statement that generates a single row into a set of user variables. VALUES
语句中选择值,该语句将单行生成一组用户变量。In this case, you must employ a table alias, and you must assign each value from the value list to a variable. 在这种情况下,必须使用表别名,并且必须将值列表中的每个值赋给一个变量。Each of the two statements shown here is equivalent to 这里显示的两个语句中的每一个都等同于SET @x=2, @y=4, @z=8
:SET @x=2, @y=4, @z=8
:
SELECT * FROM (VALUES ROW(2,4,8)) AS t INTO @x,@y,@z; SELECT * FROM (VALUES ROW(2,4,8)) AS t(a,b,c) INTO @x,@y,@z;
User variable names are not case-sensitive. 用户变量名不区分大小写。See Section 9.4, “User-Defined Variables”.请参阅第9.4节,“用户定义变量”。
The SELECT ... INTO OUTFILE '
form of file_name
'SELECT
writes the selected rows to a file. SELECT ... INTO OUTFILE '
形式将所选行写入文件。file_name
'The file is created on the server host, so you must have the 文件是在服务器主机上创建的,因此您必须具有FILE
privilege to use this syntax. FILE
权限才能使用此语法。file_name
cannot be an existing file, which among other things prevents files such as /etc/passwd
and database tables from being modified. file_name
不能是现有文件,这会阻止修改/etc/passwd
和数据库表等文件。The character_set_filesystem
system variable controls the interpretation of the file name.character_set_filesystem
系统变量控制文件名的解释。
The SELECT ... INTO OUTFILE
statement is intended to enable dumping a table to a text file on the server host. SELECT ... INTO OUTFILE
语句用于将表转储到服务器主机上的文本文件中。To create the resulting file on some other host, 要在其他主机上创建结果文件,SELECT ... INTO OUTFILE
normally is unsuitable because there is no way to write a path to the file relative to the server host file system, unless the location of the file on the remote host can be accessed using a network-mapped path on the server host file system.SELECT ... INTO OUTFILE
通常不合适,因为无法写入相对于服务器主机文件系统的文件路径,除非可以使用服务器主机文件系统上的网络映射路径访问远程主机上的文件位置。
Alternatively, if the MySQL client software is installed on the remote host, you can use a client command such as 或者,如果MySQL客户端软件安装在远程主机上,则可以使用mysql -e "SELECT ..." >
to generate the file on that host.file_name
mysql -e "SELECT ..." >
以在该主机上生成文件。file_name
SELECT ... INTO OUTFILE
is the complement of LOAD DATA
. SELECT ... INTO OUTFILE
是LOAD DATA
的补充。Column values are written converted to the character set specified in the 列值被写入并转换为CHARACTER SET
clause. CHARACTER SET
子句中指定的字符集。If no such clause is present, values are dumped using the 如果不存在这样的子句,则使用binary
character set. binary
字符集转储值。In effect, there is no character set conversion. 实际上,没有字符集转换。If a result set contains columns in several character sets, so is 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. export_options
部分的语法由LOAD DATA
语句使用的相同FIELDS
子句和LINES
子句组成。For information about the 有关FIELDS
and LINES
clauses, including their default values and permissible values, see Section 13.2.7, “LOAD DATA Statement”.FIELDS
子句和LINES
子句的信息,包括其默认值和允许值,请参阅第13.2.7节,“加载数据语句”。
FIELDS ESCAPED BY
controls how to write special characters. FIELDS ESCAPED BY
控制了如何写入特殊字符。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:FIELDS ESCAPED BY
字符不为空,则在必要时将其用作前缀,以避免在输出时出现歧义,前缀位于以下字符之前:
The FIELDS ESCAPED BY
characterFIELDS ESCAPED BY
字段
The FIELDS [OPTIONALLY] ENCLOSED BY
characterFIELDS [OPTIONALLY] ENCLOSED BY
字符
The first character of the FIELDS TERMINATED BY
and LINES TERMINATED BY
valuesFIELDS TERMINATED BY
值和LINES TERMINATED BY
值的第一个字符
ASCII ASCII NUL
(the zero-valued byte; what is actually written following the escape character is ASCII 0
, not a zero-valued byte)NUL
(零值字节;在转义字符后面实际写入的是ASCII 0
,而不是零值字节)
The 必须转义FIELDS TERMINATED BY
, ENCLOSED BY
, ESCAPED BY
, or LINES TERMINATED BY
characters must be escaped so that you can read the file back in reliably. FIELDS TERMINATED BY
、ENCLOSED BY
、ESCAPED BY
或LINES TERMINATED BY
字符,以便可靠地读回文件。ASCII ASCII-NUL
is escaped to make it easier to view with some pagers.NUL
被转义,以便于使用某些寻呼机查看。
The resulting file need not conform to SQL syntax, so nothing else need be escaped.生成的文件不需要符合SQL语法,因此不需要转义任何其他内容。
If the 如果按FIELDS ESCAPED BY
character is empty, no characters are escaped and NULL
is output as NULL
, not \N
. FIELDS ESCAPED BY
字符为空,则不转义任何字符,并且NULL
输出为NULL
,而不是\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.指定一个空转义字符可能不是一个好主意,特别是如果数据中的字段值包含刚刚给出的列表中的任何字符。
当您要将表的所有列转储到文本文件中时,INTO OUTFILE
can also be used with a TABLE
statement when you want to dump all columns of a table into a text file. INTO-OUTFILE
还可以与TABLE
语句一起使用。In this case, the ordering and number of rows can be controlled using 在这种情况下,可以使用ORDER BY
and LIMIT
; these clauses must precede INTO OUTFILE
. ORDER BY
和LIMIT
控制行的顺序和数量;这些子句必须在INTO OUTFILE
之前。TABLE ... INTO OUTFILE
supports the same export_options
as does SELECT ... INTO OUTFILE
, and it is subject to the same restrictions on writing to the file system. ABLE ... INTO OUTFILE
支持与SELECT ... INTO OUTFILE
相同的export_options
,并且它在写入文件系统时受到相同的限制。An example of such a statement is shown here:这种说法的一个例子如下:
TABLE employees ORDER BY lname LIMIT 1000 INTO OUTFILE '/tmp/employee_data_1.txt' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"', ESCAPED BY '\' LINES TERMINATED BY '\n';
You can also use 您也可以使用SELECT ... INTO OUTFILE
with a VALUES
statement to write values directly into a file. SELECT ... INTO OUTFILE
配合VALUES
语句将值直接写入文件。An example is shown here:示例如下:
SELECT * FROM (VALUES ROW(1,2,3),ROW(4,5,6),ROW(7,8,9)) AS t INTO OUTFILE '/tmp/select-values.txt';
You must use a table alias; column aliases are also supported, and can optionally be used to write values only from desired columns. 必须使用表别名;还支持列别名,并且可以选择仅从所需列写入值。You can also use any or all of the export options supported by 您还可以使用SELECT ... INTO OUTFILE
to format the output to the file.SELECT ... INTO OUTFILE
支持的任何或所有导出选项将输出格式化为文件。
Here is an example that produces a file in the comma-separated values (CSV) format used by many programs:下面是一个以逗号分隔值(CSV)格式生成文件的示例,许多程序都使用该格式:
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. INTO DUMPFILE
而不是INTO OUTFILE
,MySQL只向文件中写入一行,没有任何列或行终止,也没有执行任何转义处理。This is useful for selecting a 这对于选择BLOB
value and storing it in a file.BLOB
值并将其存储在文件中非常有用。
TABLE
also supports INTO DUMPFILE
. TABLE
还支持INTO DUMPFILE
。If the table contains more than one row, you must also use 如果表包含多行,则还必须使用LIMIT 1
to limit the output to a single row. LIMIT 1
将输出限制为一行。INTO DUMPFILE
can also be used with SELECT * FROM (VALUES ROW()[, ...]) AS
. table_alias
[LIMIT 1]INTO DUMPFILE
还可以与SELECT * FROM (VALUES ROW()[, ...]) AS
配合使用。table_alias
[LIMIT 1]See Section 13.2.14, “VALUES Statement”.请参阅第13.2.14节,“VALUES语句”。
Any file created by 由INTO OUTFILE
or INTO DUMPFILE
is owned by the operating system user under whose account mysqld runs. INTO OUTFILE
或INTO DUMPFILE
创建的任何文件都归运行mysqld的操作系统用户所有。(You should never run mysqld as (由于这个原因和其他原因,您不应该以root
for this and other reasons.) root
身份运行mysqld。)As of MySQL 8.0.17, the umask for file creation is 0640; you must have sufficient access privileges to manipulate the file contents. 从MySQL8.0.17开始,文件创建的单元掩码是0640;您必须具有足够的访问权限才能操作文件内容。Prior to MySQL 8.0.17, the umask is 0666 and the file is writable by all users on the server host.在MySQL 8.0.17之前,umask是0666,该文件可由服务器主机上的所有用户写入。
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.secure_file_priv
系统变量设置为非空目录名,则要写入的文件必须位于该目录中。
In the context of 在作为事件调度器执行的事件的一部分发生的SELECT ... INTO
statements that occur as part of events executed by the Event Scheduler, diagnostics messages (not only errors, but also warnings) are written to the error log, and, on Windows, to the application event log. SELECT ... INTO
语句的上下文中,诊断消息(不仅是错误,还有警告)会写入错误日志,在Windows上还会写入应用程序事件日志。For additional information, see Section 25.4.5, “Event Scheduler Status”.有关更多信息,请参阅第25.4.5节,“事件调度器状态”。
As of MySQL 8.0.22, support is provided for periodic synchronization of output files written to by 从MySQL 8.0.22开始,支持定期同步SELECT INTO OUTFILE
and SELECT INTO DUMPFILE
, enabled by setting the select_into_disk_sync
server system variable introduced in that version. SELECT INTO OUTFILE
和SELECT INTO DUMPFILE
写入的输出文件,通过设置该版本中引入的select_into_disk_sync
服务器系统变量启用。Output buffer size and optional delay can be set using, respectively, 输出缓冲区大小和可选延迟可以分别使用以下选项进行设置:select_into_buffer_size
and select_into_disk_sync_delay
. select_into_buffer_size
和select_into_disk_sync_delay
。For more information, see the descriptions of these system variables.有关详细信息,请参阅这些系统变量的说明。