How do I use the @ sign in MySQL?


To use the @ sign, use MySQL SET command. The @sign is used to set user-defined variables. Following is the syntax −

SET @anyVariableName:=yourValue;

Let us first create a table −

mysql> create table DemoTable1331
   -> (
   -> Id int,
   -> Name varchar(20)
   -> );
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1331 values(10,'Chris');
Query OK, 1 row affected (0.71 sec)
mysql> insert into DemoTable1331 values(101,'David');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1331 values(40,'Bob');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1331 values(30,'Mike');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable1331 values(60,'John');
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1331;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
|   10 | Chris |
|  101 | David |
|   40 | Bob   |
|   30 | Mike  |
|   60 | John  |
+------+-------+
5 rows in set (0.00 sec)

Here is the query to use @sign in MySQL −

mysql> set @Value:=40;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from DemoTable1331 where Id > @Value;

This will produce the following output −

+------+-------+
| Id   | Name  |
+------+-------+
|  101 | David |
|  60  | John  |
+------+-------+
2 rows in set (0.03 sec)

Updated on: 05-Nov-2019

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements