我有一个表,其中包含人员 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
最佳答案
您已将 result
声明为字符串。然后你使用加法。难怪您的代码没有达到您的预期。
也许 concat()
就是您想要的:
set result = concat_ws(',', result, (select managerid from table where personid = l_loop);
关于mysql while循环返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38589976/