SELECT INTO

语法

SELECT col_name [, col_name] ...
    INTO var_name [, var_name] ...
    table_expr

描述

使用SELECT ... INTO语句可以将指定列的值直接存储到变量中。该语句不会显式输出任何结果。 该查询语句只能返回单行查询结果,如果查询没有任何返回行,则返回 1329 错误(No data),这时变量保持不变。如果查询返回多行,则返回 1172 错误(结果超出一行)。如果查询语句可能会返回多行,可以使用 LIMIT 1子句来限制结果集中只有单行。

其中INTO子句也可以指定在语句的结尾处。

如果该语句的执行上下文是事件调度器(Event Scheduler)所执行的事件一部分,那么诊断信息(不仅包括errors,还包括warning)会写入到错误日志中,在Windows上则是写入到事件日志中。更多信息请参见http://dev.mysql.com/doc/refman/5.1/en/events-status-info.html.

该语句可为本地变量 local variables 和用户变量 user-defined variables 赋值。

更详细的语法,参见 SELECT

另一种为变量赋值的方式是使用 SET 语句。

SELECT ... INTO 的结果不会缓存到 query cache 中,即使指定了 SQL_CACHE 选项。

示例

SELECT id, data INTO x,y 
FROM test.t1 LIMIT 1;

See also

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.