BEGIN
CASE x
WHEN 0 THEN (
IF thisIsAVariable = 'viewAll' THEN
SELECT id, fname FROM Persons WHERE 1
ELSE
SELECT id, fname FROM Persons WHERE id = '1111'
END IF
);
END CASE;
END
casewhen语句中ifelse语句的正确语法是什么?
答案 0 :(得分:1)
只需使用IF-ELSE
,
DELIMITER $$
CREATE PROCEDURE procName
BEGIN
IF x = 0 THEN
IF thisIsAVariable = 'viewAll' THEN
SELECT id, fname FROM Persons WHERE 1;
ELSE
SELECT id, fname FROM Persons WHERE id = '1111';
END IF;
ELSE
-- other codes if you have
END IF;
END $$
DELIMITER ;
我认为在使用CASE
时,select语句应该只返回一个值。
答案 1 :(得分:1)
我们可以使用if if里面的代码,如下所示:
BEGIN
IF x = 0 THEN
IF thisIsAVariable = 'viewAll' THEN
SELECT id, fname FROM Persons WHERE 1;
ELSE
SELECT id, fname FROM Persons WHERE id = '1111';
END IF;
END IF;
END
答案 2 :(得分:0)
试试这个
BEGIN
CASE x
WHEN 0 THEN (
SELECT id, fname FROM Persons WHERE id = IF(thisIsAVariable = 'viewAll',1,'1111')
);
END CASE;
END