即使数据库中存在匹配的数据,以下存储过程也不返回任何内容。
DELIMITER $$
DROP PROCEDURE IF EXISTS `portaldb`.`hasPrivateCloud`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `hasPrivateCloud`(in_userId int)
begin
if in_userId is not null then
select (case when ui.privateCloud is null
then false
else ui.privateCloud
end) as hasPrivateCloud from userinfo as ui where ui.userid = in_userId;
end if;
end$$
DELIMITER ;
privateCloud
是一个 bool 字段。如果字段为 false 或不满足 where 子句,则过程应返回 false,而当满足 where 子句时,则应返回列值。但它总是返回一个空列。
这可能是什么原因?
最佳答案
当记录不存在时,不返回任何内容。不像您假设的那样,是 NULL
记录。
你可以这样写
IF EXISTS (SELECT privateCloud FROM userinfo WHERE userid = in_userId)
SELECT privateCloud FROM userinfo WHERE userid = in_userId;
ELSE
SELECT 'false';
END IF;
当您如此关心性能时,您甚至可以这样做
IF EXISTS (SELECT @result := privateCloud FROM userinfo WHERE userid = in_userId)
SELECT @result;
ELSE
SELECT 'false';
END IF;
关于mysql - 过程总是返回空列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40653045/