MySQL: Set user variable from result of query -
is possible set user variable based on result of query in mysql?
what want achieve (we can assume both user
, group
unique):
set @user = 123456; set @group = select group user user = @user; select * user group = @group;
please note know it's possible not wish nested queries.
yes, need move variable assignment query:
set @user := 123456; select @group := `group` user user = @user; select * user `group` = @group;
test case:
create table user (`user` int, `group` int); insert user values (123456, 5); insert user values (111111, 5);
result:
set @user := 123456; select @group := `group` user user = @user; select * user `group` = @group; +--------+-------+ | user | group | +--------+-------+ | 123456 | 5 | | 111111 | 5 | +--------+-------+ 2 rows in set (0.00 sec)
note set
, either =
or :=
can used assignment operator. inside other statements, assignment operator must :=
, not =
because =
treated comparison operator in non-set statements.
update:
further comments below, may following:
set @user := 123456; select `group` user limit 1 @group; select * user `group` = @group;
Comments
Post a Comment