Mysql使用SELECT CASE的结果存储变量

时间:2011-10-24 03:19:38

标签: mysql variables select case store

我正在学习如何使用SELECT CASE,但我不太了解,这里是代码:

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;

它给了我以下错误:

[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 个答案:

答案 0 :(得分:6)

您需要使用SELECT ... INTO

SELECT
...
INTO var_name [, var_name]]

而不是set将SELECT的结果赋给变量。所以这应该这样做:

SELECT CASE 
WHEN @vgls=@lgls THEN "emp"
WHEN @vgls>@lgls THEN "loc"
WHEN @vgls<@lgls THEN "vis" END
INTO @st;