2016-11-17 63 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;