我尝试做这样的事情
SET parentid = (SELECT parent FROM pages WHERE id = pageid);
CASE WHEN ( parentid <> 0 ) THEN
SET mainid = (SELECT parent FROM pages WHERE id = parentid);
ELSE
SET mainid = 0;
END
但出于某种原因,我得到了
'CASE WHEN(parentid&lt;&gt; 0)附近的语法错误THEN SET mainid = (在第3行选择父FROM FROM WHERE'
我错过了什么?
答案 0 :(得分:1)
最后没有END CASE
子句和缺少分号:
CASE WHEN ( parentid <> 0 ) THEN
SET mainid = (SELECT parent FROM pages WHERE id = parentid);
ELSE
SET mainid = 0;
END CASE;
请阅读文档以了解正确的语法:http://dev.mysql.com/doc/refman/5.0/en/case.html
以下是工作演示的链接:http://www.sqlfiddle.com/#!2/9495d1/2
答案 1 :(得分:0)
我觉得这不需要变量...
SELECT CAST(CASE
WHEN id = pageid AND parent = 0
THEN 0
ELSE (SELECT parent FROM pages WHERE id = parentid)
END AS INTEGER) AS mainid
FROM pages