The SELECT ... INTO form of SELECT enables a query result to be stored in variables or written to a file:
SELECT ……INTO SELECT的形式允许查询结果存储在变量中或写入文件:
-
SELECT ... INTO
selects column values and stores them into variables.var_list
-
SELECT
……INTO var_list选择列值并将它们存储到变量中。 -
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 OUTFILE将选中的行写入文件。可以指定列和行结束符以产生特定的输出格式。 -
SELECT ... INTO DUMPFILE
writes a single row to a file without any formatting. -
SELECT
……INTO
DUMPFILE在不进行任何格式化的情况下将一行写入文件。
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
. 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. Example:在SELECT的末尾。例子:
SELECT * FROM t1 FOR UPDATE INTO @myvar;
The INTO
position at the end of the statement is supported as of MySQL 8.0.20, and is the preferred position. 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. In other words, INTO
after FROM
but not at the end of the SELECT produces a warning.
在MySQL 8.0.20中,语句末尾的INTO位置是受支持的,并且是首选位置。锁子句之前的位置在MySQL 8.0.20中已被弃用;希望在未来的MySQL版本中删除对它的支持。换句话说,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. There are also constraints on the use of INTO
within UNION statements; see Section 13.2.10.3, “UNION Clause”.
不应该在嵌套的SELECT中使用INTO子句,因为这样的SELECT必须将其结果返回到外部上下文。在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. (Within a preparedSELECT ... INTO
statement, only user-defined variabvar_list