If the variable has not been used previously, that value is NULL:
mysql>
mysql> CREATE TABLE mytable (thing VARCHAR(20), legs INT, arms INT);
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('human',2,2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('insect',6,0);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('squid',0,10);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('octopus',0,8);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('fish',0,0);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('centipede',100,0);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('table',4,0);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('armchair',4,2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('phonograph',0,1);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('tripod',3,0);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('Peg Leg Pete',1,2);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO mytable (thing,legs,arms) VALUES('space alien',NULL,NULL);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> SELECT @name2 := thing FROM mytable WHERE legs < 0;
Empty set (0.00 sec)
mysql> SELECT @name2;
+--------+
| @name2 |
+--------+
| NULL |
+--------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category