You can store a value in a user-defined variable in one statement and refer to it later in another statement. This enables you to pass values from one statement to another.
可以先在一个语句中保存用户变量的值,然后再另一个语句中引用它。这样可以将值从一个语句传递到另一个语句。
User variables are written as @var_name, where the variable name var_name consists of alphanumeric characters, ., _, and $. A user variable name can contain other characters if you quote it as a string or identifier (for example, @'my-var', @"my-var", or @`my-var`).
用户变量的形式是 @var_name,其中变量名var_name可以由当前字符集的文字数字(alphanumeric)字符、‘.’、‘_’和‘$’组成。 把当变量名当作字符串或标识符引用的时候,变量名也可以包含其它的字符(比如@'my-var', @"my-var", @`my-var`)
User-defined variables are session specific. A user variable defined by one client cannot be seen or used by other clients. (Exception: A user with access to the Performance Schema user_variables_by_thread table can see all user variables for all sessions.) All variables for a given client session are automatically freed when that client exits.
用户变量仅在当前会话中生效。当前会话中的用户变量不能被其它会话访问(例外情况:访问Performance Schema库中user_variables_by_thread 表是)。当会话关闭后,用户变量就会失效。
User variable names are not case-sensitive. Names have a maximum length of 64 characters.
用户变量名对大小写不敏感,最长不超过64字符
One way to set a user-defined variable is by issuing a SET statement:
设置用户变量的一个途径是通过SET语句:
SET @var_name = expr [, @var_name = expr] ...
For SET, either = or := can be used as the assignment operator.
当用SET语句设置用户变量的时候,可以使用“=”或者“:=”操作符
You can also assign a value to a user variable in statements other than SET. In this case, the assignment operator must be := and not = because the latter is treated as the comparison operator = in non-SET statements:
也可以不用SET语句来设置用户变量,在这张情况下,必须使用“=”操作符
mysql> SET @t1=1, @t2&