过程始终返回空列

时间:2016-11-17 10:55:49

标签: mysql stored-procedures

即使数据库中存在匹配数据,以下存储过程也不会返回任何内容。

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

它可能是什么原因?

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

对于之前的帖子感到抱歉。我已经把它删了。感谢@fancyPants指出这一点。

以为我会再添加一个建议

>>> list_c = []
>>> for a, b in zip(list_a, list_b):
...    list_c.append(a.replace('x', b))
...
>>> list_c
['file_1', 'file_2', 'file_3', 'file_4']
相关问题