2016-11-17 65 views
0

即使数据库中存在匹配数据,以下存储过程也不会返回任何结果。过程始终返回空列

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是一个布尔字段。如果该字段为假或where子句不满足,则过程应返回false,并且where子句满足时,它应返回列值。但它总是返回一个空列。

可能是什么原因呢?

+0

也许这下面计算器后 [在选择显示位值(http://stackoverflow.com/questions/14248554/cant-see-mysql- bit-field-value-when-select-select)会帮助 – g3suya

回答

1

当记录不存在时,不会返回任何内容。不是,就像你所假设的那样,一个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; 
+0

好的。但即使在privateCloud为false的情况下,它也会返回true。 –

+0

修复了........ – fancyPants

+0

有没有比运行查询两次更好的方法? –

0

我们对此深感抱歉较早前的职位。我删了它。感谢@fancyPants指出了这一点。

以为我会添加更多的建议,但

select (case when ifnull(ui.privateCloud,false)=false 
       then 'False' 
       else 'True' 
       end) as hasPrivateCloud from userinfo as ui where ui.userid= in_userId;