Home > Database > Mysql Tutorial > body text

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

WBOY
Release: 2023-09-03 17:01:06
forward
1248 people have browsed it

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]…
Copy after login

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template