gpt4 book ai didi

mysql - MYSQL中if else if嵌套的写法

转载 作者:可可西里 更新时间:2023-11-01 06:31:36 28 4
gpt4 key购买 nike

以下语法接缝是正确的。在 mysql 上运行时出现错误

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 27".

delimiter $$
create function check2_login(p_username varchar(30),p_password varchar(30),role varchar(20))
returns bool
deterministic
begin
declare loginstatus bool default false;

if role="customer"then
select custid from customer where custid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else if role="executive"then
select execid from executive where execid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else if role="admin"then
select empid from employee where empid=p_username and pwd=p_password;
if !row_count()=0 then
select true into loginstatus;
end if;
else
return loginstatus;
end if;
return loginstatus;
end $$

最佳答案

如下更新你的存储函数

DELIMITER $$
DROP FUNCTION IF EXISTS `check2_login`$$
CREATE FUNCTION `check2_login`(p_username VARCHAR(30),p_password VARCHAR(30),role VARCHAR(20)) RETURNS BOOL
BEGIN
DECLARE retval INT;
IF role = "customer" THEN
SELECT COUNT(custid) INTO retval FROM customer WHERE custid = p_username and pwd = p_password;
IF retval != 0 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
ELSEIF role = "executive" THEN
SELECT COUNT(execid) INTO retval FROM executive WHERE execid = p_username and pwd = p_password;
IF retval != 0 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
ELSEIF role = "admin" THEN
SELECT COUNT(empid) INTO retval FROM employee WHERE empid = p_username and pwd = p_password;
IF retval != 0 THEN
RETURN TRUE;
ELSE
RETURN FALSE;
END IF;
ELSE
RETURN FALSE;
END IF;
END$$
DELIMITER ;

希望这对您有所帮助...!

关于mysql - MYSQL中if else if嵌套的写法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21676224/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com