问题
im learning how to use the SELECT CASE but i don't understand very well, here is the code:
SET @vgls=1;
SET @lgls=1;
SET @st=SELECT CASE
WHEN @vgls=@lgls THEN "emp"
WHEN @vgls>@lgls THEN "loc"
WHEN @vgls<@lgls THEN "vis" END;
SELECT @st;
It gives me the following error:
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to you MySQL server version for the right syntax to use near 'SELECT CASE
WHEN @vgls=@lgls THEN "emp"
WHEN @vgls>@lgls THEN "loc"
WHEN @vg' at line 1
回答1:
You need to use SELECT ... INTO
SELECT
...
INTO var_name [, var_name]]
instead of set
to assign the result of a SELECT to a variable. So this should do it:
SELECT CASE
WHEN @vgls=@lgls THEN "emp"
WHEN @vgls>@lgls THEN "loc"
WHEN @vgls<@lgls THEN "vis" END
INTO @st;
来源:https://stackoverflow.com/questions/7871014/mysql-storing-a-variable-with-the-result-of-an-select-case