题目表达不清楚,先大体描述一下场景:
一张表 t(tid,parent);其中如果praent 不存在那么这个字段为null;典型的树形结构的数据。
其中的层次不确定。也就是说可以是child->parent->pareng....
不确定最终某个child的头上有几层parent.
现在需要取出所有的id和其最顶级的点的对应关系数据。
Sql如下:
select (select tid from t where parent is null start with tid=a.tid connect by prior parent=tid ) as topId,a.tid as tid from t a