如何在 MySQL 中为普通查询声明一个变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10893517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:40:05  来源:igfitidea点击:

How to declare a variable in MySQL for a normal query?

mysqlvariablesvariable-declaration

提问by EOB

How can I declare a variable for a normal query in MySQL?

如何在 MySQL 中为普通查询声明变量?

e.g.,

例如,

declare @myVar date;
set @myVar = something;

select * from someTable where someColumn = @myVar;

I tried and the syntax seems to be wrong...what am I missing?

我试过了,语法似乎是错误的……我错过了什么?

回答by aleroot

You can declare a session variable in this way :

您可以通过这种方式声明会话变量:

SET @myvarname := 'value';

or a local variable in this way :

或这样的局部变量:

DECLARE my_variable varchar(30)

also:

还:

DECLARE my_variable varchar(30) DEFAULT 'value'