Mega Code Archive

 
Categories / MySQL / Select Clause
 

SQL variables hold single values

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> mysql> SELECT @name := thing FROM mytable WHERE legs = 0; +----------------+ | @name := thing | +----------------+ | squid          | | octopus        | | fish           | | phonograph     | +----------------+ 4 rows in set (0.00 sec) mysql> SELECT @name; +------------+ | @name      | +------------+ | phonograph | +------------+ 1 row in set (0.00 sec) mysql> mysql> drop table mytable; Query OK, 0 rows affected (0.00 sec)