需要在SQL中将两列数据合并为一列?

时间:2013-01-08 05:58:31

标签: sql oracle

我在这里提供一个例子让大家都明白我真正需要的东西: 我有一张这样的桌子:

 Name                                      Null?    Type
 ----------------------------------------- -------- --------------
 Child_ID                                               NUMBER(10)
 Father_ID                                              NUMBER(10)

值为:

    Child_ID       Father_ID
----------         ----------
         2          1
         4          1
         3          2
         5          3

现在我想要父id的分层信息1.为此,我写了一个查询,它为我提供了确切的输出:

**select * from child
start with father_id=1
connect by prior child_id = father_id;**

O / P:

      Child_ID       Father_ID
----------           ----------
         2            1
         3            2
         5            3
         4            1

现在我希望o / p应该是这样的:

ID
-----
1
2
3
4
5

我可以通过使用union键轻松获得它,但我不想使用它。 有没有其他方法可以得到这个? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

这有点乱,但你可以这样做:

select distinct case when l=1 then child_id else father_id end id
from
    (select child_id, father_id
     from child
     start with father_id=1 connect by prior child_id=father_id) child
     cross join (select level l from dual connect by level < 3)
order by id
;
相关问题