有如下图的表,现在要根据其中一个id分别查到它的所有父类和所有子类。
--由父类得到所有子类
with f as
(
select * from t where id=3
union all
select a.* from t a inner join f on a.fid=f.id
)
select * from f
--由子类得到所有父类
with f as
(
select * from t where id=3
union all
select a.* from t a inner join f on a.id=f.fid
)
select * from f order by id asc