Mysql 学习笔记
syntax
set
[Reference](http://dev.mysql.com/doc/refman/5.5/en/user-variables.html)
-
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.
-
Syntax:
SET @var_name = expr [, @var_name = expr] ...
-
Principles: 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
). -
For SET, either = or := can be used as the assignment operator.
-
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语句来说: = 和 := 都是给变量赋值,
# 但是 如果不在SET语句下就只能用 := 给变量赋值了,因为此时 = 号是比较而不是赋值。
set @adult_age = 18;
select @adult_age ,@adult_age = 20 , @adult_age = 18, @adult_age := 20 , @adult_age;
输出:
18 0 1 20 20
-
User variables can be assigned a value from a limited set of data types: integer, decimal, floating-point, binary or nonbinary string, or NULL value.
-
Life-Cycle: User-defined variables are session specific.
A user variable defined by one client cannot be seen or used by other clients.
All variables for a given client session are automatically freed when that client exits.