Home  >  Article  >  Database  >  How do we store values ​​in user defined variables?

How do we store values ​​in user defined variables?

WBOY
WBOYforward
2023-09-03 17:01:061198browse

How do we store values ​​in user defined variables?

#We can store a value in a user-defined variable in a statement and then reference it in other statements. Here is how to store the value of a user-defined variable:

Using the SET statement

We can store a user-defined variable by issuing a SET statement as follows:

Syntax

SET @var_name = expr[, @var_name = expr]…

In this sentence, @var_name is the variable name, consisting of alphanumeric characters in the current character set. We can use = or := assignment operator with SET statement.

For example, the following query can use the SET statement to store user variables −

mysql> SET @value = 500;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @value := 500;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @value = 500, @value1=550;
Query OK, 0 rows affected (0.00 sec)

There is no SET statement

There is no SET statement. We can also assign values ​​to user variables in the statement as follows −

mysql> select @value,@value1, @value2 := @value+@value1;

+--------+---------+---------------------------+
| @value | @value1 | @value2 := @value+@value1 |
+--------+---------+---------------------------+
| 500    | 550     | 1050                      |
+--------+---------+---------------------------+
1 row in set (0.00 sec)

In this case, we must use the := assignment operator.

The above is the detailed content of How do we store values ​​in user defined variables?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete