简体   繁体   English

如何在Oracle中从两个表中选择所有记录,“ Child as Parent”,直到没有子记录成为Parent?

[英]How to Select all records, Child as Parent from two tables until no child record as parent , in Oracle?

May be my title description is not clear for you :-) But, below is example. 可能是我的标题描述不清楚:-)但是,下面是示例。

I have two tables, 我有两张桌子

Table 1: 表格1:

SNO   PARENTID   
1      P01
2      P02
3      P03
4      P04

Table 2: 表2:

SNO   CHILDID   PARENTID
1      C01        P01
2      C02        P01
3      C03        P03
4      P03        P01
5      P04        P01
6      A01        P03
7      A02        P04

So, each ParentId has multiple childs like 因此,每个ParentId都有多个子项,例如

select childid from table2 where parentid = 'P01';

I will get C01,C02,P03 AND P04 . 我会得到C01,C02,P03 AND P04 This is normal, 这很正常,

But in my case I want to again check these childids with my table1(parent table) , if present then again I need to the childids of those parentids also. 但是在我的情况下,我想再次使用table1(parent table)检查这些childids ,如果存在,那么我也需要这些parentidschildids it will continue until my childid does not match with my parentid . 它会继续下去,直到我childid不匹配我parentid

Ex Cont.. Above my output contains P03 and P04, so I need to take the childids C03,A01 AND A02 also... Ex Cont ..在我的输出上方包含P03和P04,因此我还需要带孩子C03,A01和A02 ...

I need query for this,help me guys. 我需要查询,帮帮我。

Thanks in Advance, Stephen.L 在此先感谢Stephen.L

I'm not quite clear how you want to layout the result set but you should use Oracle's hierarchical query syntax: 我不太清楚您如何布局结果集,但是您应该使用Oracle的分层查询语法:

SQL> select t2.parentid
  2         , lpad(' ', (level-1)*2)||t2.childid as childid
  3         , level
  4  from t2 
  5  connect by prior t2.childid = t2.parentid
  6  start with t2.parentid = 'P01'
  7  /

PARE CHILDID         LEVEL
---- ---------- ----------
P01  C01                 1
P01  C02                 1
P01  P03                 1
P03    A01               2
P03    C03               2
P01  P04                 1
P04    A02               2

7 rows selected.

SQL> 

Here I have ignored T1 as T2 has the information necessary to create the hierarchy. 这里我忽略了T1,因为T2具有创建层次结构所需的信息。 You can join to T1 in the query, but your question isn't clear what role it plays. 您可以在查询中加入T1,但您的问题尚不清楚该角色。 By not joining to it we lose the entry for P02. 如果不加入,我们将丢失P02的条目。 However, joining with T1 will generate multiple rows for P03 and P04. 但是,与T1联接将为P03和P04生成多个行。

SQL> select t1.parentid
 2          , lpad(' ', level*2)||t2.childid as childid
 3          , level
 4   from t1
 5      left join t2 on t1.parentid=t2.parentid
 6   connect by prior t2.childid = t2.parentid
 7   start with t1.parentid is not null
 8   order by t1.parentid
 9   /

PARE CHILDID         LEVEL
---- ---------- ----------
P01    C01               1
P01    C02               1
P01    P03               1
P01    P04               1
P02                      1
P03      C03             2
P03    A01               1
P03    C03               1
P03      A01             2
P04      A02             2
P04    A02               1

11 rows selected.

SQL> 

Oracle offers some highly neat extensions to SQL to work with hierarchical data. Oracle为SQL提供了一些非常整洁的扩展,以使用分层数据。 They are definitely work exploring. 他们肯定是在探索工作。 Find out more . 了解更多

You can try this: 您可以尝试以下方法:

select CHILDID from tbl2 t2 
join tbl1 t1 on t2.PARENTID = t1.PARENTID
START WITH t2.parentid = 'P01'
CONNECT BY NOCYCLE PRIOR CHILDID = t2.parentid;

Here if the CHILDID retrieved from tbl2 doesn't exists in tbl1 , then it won't search for its children 在这里,如果从CHILDID检索到的tbl2tbl1中不存在,则它不会搜索其children

Here Oracle hierarchical query is used for more info check this link 在此使用Oracle分层查询获取更多信息,请检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM