我有这个SQL查询:
CREATE DEFINER=`root`@`localhost` FUNCTION `user_login_fn`(username varchar(32),pass varchar(128),actual_user int) RETURNS tinyint(1)
BEGIN
declare compareResult boolean;
declare user_id int;
if (select EXISTS(SELECT * FROM `user` WHERE `username`= username and `password` = sha2(pass, 512) and `status`='A')) THEN
SELECT id_user into user_id FROM `user` WHERE `username`= username and `password` = sha2(pass, 512) and `status`='A';
SET compareResult = register_insert_fn(actual_user, 5, Concat("Login user with ID: ", user_id));
return true;
else
SELECT id_user into user_id FROM `user` WHERE `username`= username;
SET compareResult = register_insert_fn(actual_user, 5, Concat("Failed login user with ID: ", user_id));
return false;
END IF;
END
即使我传递了null参数,问题总是返回TRUE。 我像这样测试Select stament:
select EXISTS(SELECT * FROM `user` WHERE `username`= 'prueba' and `password` = sha2('123', 512) and `status`='A')
工作正常,但在函数中总是返回True或1。
答案 0 :(得分:0)
该函数返回true或false,因为在if
的true分支中代码返回true,而在else
分支中代码返回false。没有其他返回值。如果您希望函数返回其他一些值,则需要更改实现的逻辑并确保还返回其他值。