Home > Database > Mysql Tutorial > body text

What do MySQL user variables mean? How do we assign values ​​to them?

PHPz
Release: 2023-09-05 08:09:02
forward
869 people have browsed it

MySQL 用户变量是什么意思?我们如何为它们赋值?

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.

Example
mysql> SET @A = 100;
Query OK, 0 rows affected (0.00 sec)

mysql> Set @B = 'MySQL';
Query OK, 0 rows affected (0.00 sec)
Copy after login

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

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

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

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!

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