Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Storing a variable with the result of an SELECT CASE

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
like image 400
Enrique Benitez Avatar asked Feb 22 '23 15:02

Enrique Benitez


1 Answers

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;
like image 65
mu is too short Avatar answered Mar 05 '23 15:03

mu is too short