MySQL supports user variables as of version 3.23.6. You can store a value in a user variable and refer to it later, which allows you to pass values from one statement to another. User variables are connection-specific. That is, a variable defined by one client cannot be seen or used by other clients. All variables for a client connection are automatically freed when the client exits.
User variables are written as @var_name, where the variable name var_name may consist of alphanumeric characters from the current character set, _, $, and . . The default character set is ISO-8859-1 (Latin1). This may be changed with the --default-character-set option to mysqld. See Character sets. User variable names are not case insensitive beginning with MySQL 5.0. Before that, they are case sensitive.
One way to set a user variable is by issuing a SET statement:
SET @var_name = expression [,@var_name = expression] ...
The expression assigned to the variable can evaluate to an integer, real, string, or NULL value.
You can also assign a value to a user variable in statements other than SET. However, in this case, the assignment operator is := rather than =, because = is treated as a comparison operator in non-SET statements:
mysql> SET @t1=0, @t2=0, @t3=0; mysql> SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3; +----------------------+------+------+------+ | @t1:=(@t2:=1)+@t3:=4 | @t1 | @t2 | @t3 | +----------------------+------+------+------+ | 5 | 5 | 1 | 4 | +----------------------+------+------+------+
User variables may be used where expressions are allowed. However, this does not currently include contexts that explicitly require a number, such as in the LIMIT clause of a SELECT statement, or the IGNORE number LINES clause of a LOAD DATA statement.
If you refer to a variable that has not been initialized, its value is NULL.
Note: In a SELECT statement, each expression is evaluated only when it's sent to the client. This means that in the HAVING, GROUP BY, or ORDER BY clause, you can't refer to an expression that involves variables that are set in the SELECT part. For example, the following statement will not work as expected:
mysql> SELECT (@aa:=id) AS a, (@aa+3) AS b FROM table_name HAVING b=5;
The reason is that @aa will not contain the value of the current row, but the value of id from the previous selected row.
The general rule is to never assign and use the same variable in the same statement.
Another issue with setting a variable and using it in the same statement is that the default result type of a variable is based on the type of the variable at the start of the statement. The following example illustrates this:
mysql> SET @a="test"; mysql> SELECT @a,(@a:=20) FROM table_name;
For the SELECT statement, MySQL will report to the client that column one is a string and convert all accesses of @a to strings, even if @a will be set to a number for the second row. After the SELECT statement is executed, @a will be regarded as a number for the next statement.
An unassigned variable has a value of NULL with a type of string.
To avoid problems with this behavior, either do not set and use the same variable within a single statement, or else set the variable to 0, 0.0, or '' to define its type before you use it.