Variables can be assigned values from arbitrary expressions.
mysql>
mysql> CREATE TABLE mytable (thing VARCHAR(20), legs INT, arms INT);
Query OK, 0 rows affected (0.00 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 @max_mytable := MAX(arms+legs) FROM mytable;
+--------------------------------+
| @max_mytable := MAX(arms+legs) |
+--------------------------------+
| 100 |
+--------------------------------+
1 row in set (0.00 sec)
mysql>
mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)
Related examples in the same category