多层级组织树,怎么通过多个id,查询出其所有的父级

多层级组织树,怎么通过多个id,查询出其所有的父级, java怎么实现? 或者mysql就能直接写出查询语句吗?

子级都会保存有父级的id的,通过SQL就能试下

oracle :start with connect by

递归

img

外部循环调一个这样的方法

with recursive cte(id,pid) as (
    select d1.id,d1.pid from dev.dept d1 where id in (4,5,6)
    union all
    select d2.id,d2.pid from cte c, dev.dept d2 where c.pid=d2.id 
    )
select * from dept where id in (select id from cte);