MySQL user variables are written as @variable, which can be set to an integer, real number, string, or NULL value. With the help of SET statement, we can assign values to user variables. While assigning a value to a user variable, we can use = or := as assignment operator.
mysql> SET @A = 100; Query OK, 0 rows affected (0.00 sec) mysql> Set @B = 'MySQL'; Query OK, 0 rows affected (0.00 sec)
The above query assigns the value 100 to user variable A and "MySQL" to user variable B respectively.
We can check their values with the help of SELECT statement as shown below-
mysql> Select @A, @B; +------+-------+ | @A | @B | +------+-------+ | 100 | MySQL | +------+-------+ 1 row in set (0.05 sec)
We can also perform multiple variable assignments separated by commas. The following example illustrates this -
mysql> SET @X = 'Ram',@Y = 'Shyam',@Z = 'Students', @S = 5000; Query OK, 0 rows affected (0.00 sec) mysql> Select @X, @Y, @Z, @S; +------+-------+----------+------+ | @X | @Y | @Z | @S | +------+-------+----------+------+ | Ram | Shyam | Students | 5000 | +------+-------+----------+------+ 1 row in set (0.00 sec)
We can also assign values to them using SELECT statement, but for this we have to use only := assignment operator instead of =. As shown in the following example -
mysql> SELECT @C := 'Tutorialspoint'; +------------------------+ | @C := 'Tutorialspoint' | +------------------------+ | Tutorialspoint | +------------------------+ 1 row in set (0.00 sec) mysql> SELECT @C; +----------------+ | @C | +----------------+ | Tutorialspoint | +----------------+ 1 row in set (0.00 sec)
The above is the detailed content of What do MySQL user variables mean? How do we assign values to them?. For more information, please follow other related articles on the PHP Chinese website!