-- =======================向上递归=====================
delimiter $$
drop function if exists get_child_list$$
create function get_child_list(in_id varchar(50)) returns varchar(4000)
begin
declare ids varchar(4000) default '';
declare tempids varchar(4000);
set tempids = in_id;
while tempids is not null do
-- 第一步,将输入的当前节点先保存到返回集合中
set ids = CONCAT_WS(',',ids,tempids);
-- 第二步:将查询出来的数据存放到临时保存集合用于查询递归
select GROUP_CONCAT(id) into tempids from dept where FIND_IN_SET(pid,tempids)>0;
end while;
return ids;
end
$$
delimiter ;
SELECT * from dept where FIND_IN_SET(id,get_child_list('1003'));
-- ===&