Mysql Storing a variable with the result of an SELECT CASE

て烟熏妆下的殇ゞ 提交于 2019-12-30 10:52:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!