Mega Code Archive

 
Categories / MySQL / Select Clause
 

Variable Assignment

mysql> mysql> CREATE TABLE mytable (     ->     id INT AUTO_INCREMENT,     ->     begintime DATETIME,     ->     endtime DATETIME,     ->     PRIMARY KEY (id)     -> ); Query OK, 0 rows affected (0.01 sec) mysql> mysql> INSERT INTO mytable (begintime, endtime) VALUES     ->         ('2005-03-27 7:15', '2005-03-27 18:00'),     ->         ('2005-03-28 8:00', '2005-03-28 18:00'),     ->         ('2005-03-29 7:30', '2005-03-29 16:50'),     ->         ('2005-03-30 7:00', '2005-03-30 17:15'); Query OK, 4 rows affected (0.00 sec) Records: 4  Duplicates: 0  Warnings: 0 mysql> mysql> mysql> SET @varname = 3; Query OK, 0 rows affected (0.00 sec) mysql> SELECT @varname := 3; +---------------+ | @varname := 3 | +---------------+ |             3 | +---------------+ 1 row in set (0.00 sec) mysql> SELECT @varname := COUNT(*) FROM mytable; +----------------------+ | @varname := COUNT(*) | +----------------------+ |                    4 | +----------------------+ 1 row in set (0.00 sec) mysql> SELECT COUNT(*) FROM mytable INTO @varname; Query OK, 1 row affected (0.00 sec) mysql> SELECT id, begintime FROM mytable WHERE id=1 INTO @t, @st; Query OK, 1 row affected (0.00 sec) mysql> mysql> mysql> drop table mytable; Query OK, 0 rows affected (0.00 sec)