MySQL DECLARE 中的 SELECT INTO 变量导致语法错误?
我想将单个值选择到变量中。我尝试执行以下操作:
DECLARE myvar INT(4);
-- 立即返回一些语法错误。
SELECT myvalue
FROM mytable
WHERE anothervalue = 1;
-- 返回单个整数
SELECT myvalue
INTO myvar
FROM mytable
WHERE anothervalue = 1;
-- 不起作用,也尝试过 @myvar
是否可以在存储过程或函数之外使用 DECLARE?
也许我只是不明白用户变量的概念...我只是尝试过:
SELECT myvalue INTO @var FROM `mytable` WHERE uid = 1;
SELECT @var;
...它的工作原理就像它应该的那样。但如果我一次运行每个查询,我只会得到@var NULL。
I´d like to SELECT a single value into a variable. I´d tried to following:
DECLARE myvar INT(4);
-- immediately returns some syntax error.
SELECT myvalue
FROM mytable
WHERE anothervalue = 1;
-- returns a single integer
SELECT myvalue
INTO myvar
FROM mytable
WHERE anothervalue = 1;
-- does not work, also tried @myvar
Is possible to use DECLARE outside of stored procedures or functions?
Maybe I just dont get the concept of user variables... I just tried:
SELECT myvalue INTO @var FROM `mytable` WHERE uid = 1;
SELECT @var;
...which worked just like it´s supposed to. But if I run each query at a time i just get @var NULL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我遇到了同样的问题,但我想我知道是什么导致了混乱。如果您使用 MySQL 查询分析器,则可以很好地做到这一点:
但是,如果您将相同的查询放入 MySQL Workbench,它将引发语法错误。我不知道为什么他们会有所不同,但他们确实不同。
要解决 MySQL Workbench 中的问题,您可以像这样重写查询:
I ran into this same issue, but I think I know what's causing the confusion. If you use MySQL Query Analyzer, you can do this just fine:
However, if you put that same query in MySQL Workbench, it will throw a syntax error. I don't know why they would be different, but they are.
To work around the problem in MySQL Workbench, you can rewrite the query like this:
最后,存储过程解决了我的问题。
这是有帮助的:
In the end a stored procedure was the solution for my problem.
Here´s what helped:
这些答案并不能很好地涵盖多个变量。
在存储过程中执行内联分配会导致这些结果也被发送回结果集中。这可能会令人困惑。要对多个变量使用 SELECT...INTO 语法,您需要执行以下操作:
SELECT 必须仅返回 1 行,因此 LIMIT 1,尽管这并不总是必要的。
These answers don't cover very well MULTIPLE variables.
Doing the inline assignment in a stored procedure causes those results to ALSO be sent back in the resultset. That can be confusing. To using the SELECT...INTO syntax with multiple variables you do:
The SELECT must return only 1 row, hence LIMIT 1, although that isn't always necessary.
您还可以使用 SET 代替 DECLARE
You can also use SET instead of DECLARE
根据 MySQL 文档 DECLARE 仅在 BEGIN 开始时有效...END 块就像在存储的程序中一样。
Per the MySQL docs DECLARE works only at the start of a BEGIN...END block as in a stored program.
您不需要在 MySQL 中声明变量。变量的类型在第一次赋值时自动确定。它的类型可以是以下之一:整数、小数、浮点、二进制或非二进制字符串,或者 NULL 值。有关详细信息,请参阅用户定义变量文档:
http:// dev.mysql.com/doc/refman/5.0/en/user-variables.html
您可以使用 SELECT ... INTO 将列分配给变量:
http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html示例
:
You don't need to DECLARE a variable in MySQL. A variable's type is determined automatically when it is first assigned a value. Its type can be one of: integer, decimal, floating-point, binary or nonbinary string, or NULL value. See the User-Defined Variables documentation for more information:
http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
You can use SELECT ... INTO to assign columns to a variable:
http://dev.mysql.com/doc/refman/5.0/en/select-into-statement.html
Example:
我在 Windows Vista 上使用版本 6(MySQL Workbench Community (GPL) for Windows version 6.0.9 revision 11421 build 1170)。我对以下选项没有问题。也许自从这些人三年前出现问题以来他们就解决了这个问题。
上面的所有选项都给了我正确的结果。
I am using version 6 (MySQL Workbench Community (GPL) for Windows version 6.0.9 revision 11421 build 1170) on Windows Vista. I have no problem with the following options. Probably they fixed it since these guys got the problems three years ago.
All option above give me a correct result.
值得注意的是,尽管您可以
SELECT INTO
全局变量,例如:SELECT ... INTO @XYZ ...
您也可以NOT 使用
FETCH INTO
全局变量,例如:FETCH ... INTO @XYZ
看起来这不是 错误。我希望这对某人有帮助......
It is worth noting that despite the fact that you can
SELECT INTO
global variables like:SELECT ... INTO @XYZ ...
You can NOT use
FETCH INTO
global variables like:FETCH ... INTO @XYZ
Looks like it's not a bug. I hope it will be helpful to someone...
对于那些现在遇到此类问题的人,只需尝试为表添加一个别名,这应该是技巧,例如:
它对我有用。
干杯。
For those running in such issues right now, just try to put an alias for the table, this should the trick, e.g:
It worked for me.
Cheers.
您可能会错过值之前的 @ 符号,例如
select 'test' INTO @myValue
;You maybe miss the @ symbol before your value,like that
select 'test' INTO @myValue
;选择
c1、c2、c3、...
进入
@v1、@v2、@v3、...
从
表名
在哪里
健康)状况;
SELECT
c1, c2, c3, ...
INTO
@v1, @v2, @v3,...
FROM
table_name
WHERE
condition;