递归查询

表:test id(BIGINT) parent_id(BIGINT) data(INT)

create function getSumByParentId(parentId BIGINT) returns int
begin
declare result int;
with temp as(
select * from test where id = parentId
union all select test.* from temp, test where temp.Id = test.parent_id
);
select sum(data) from temp into result;
return result;
end;

select id,getSumByParentId(id) from test;

posted @ 2019-10-27 22:27  爬上巨人的肩膀  阅读(127)  评论(0编辑  收藏  举报