【问题标题】:mysql while loop returning nullmysql while循环返回null
【发布时间】:2016-11-30 02:34:45
【问题描述】:

我有一个以个人 ID 作为主键的表,其中包含他们经理的 ID。我想设置一个 while 循环,该循环将显示该人的所有经理 ID,一直显示到列表的顶部。我建立了一个while循环,但它返回null。我做错了什么?

CREATE FUNCTION `whilefunction`() RETURNS varchar(255)
BEGIN
    declare l_loop varchar(25) default '123456';
    declare result varchar(255) default '';
    while l_loop is not null do
        set result = result + (select managerid from table where personid = l_loop);
        set l_loop = (select managerid from table where personid = l_loop);
    end while;
RETURN result;
END

【问题讨论】:

    标签: mysql while-loop do-while


    【解决方案1】:

    您已将result 声明为字符串。然后你使用加法。难怪您的代码没有达到您的预期。

    也许concat() 就是你想要的:

        set result = concat_ws(',', result, (select managerid from table where personid = l_loop);
    

    【讨论】:

    • 我现在要继续努力面对掌心。非常感谢!
    猜你喜欢
    • 2019-06-07
    • 1970-01-01
    • 2015-11-17
    • 2011-05-25
    • 2016-05-29
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    相关资源
    最近更新 更多